C语言 实现 peek() 函数

  1. c++中的peek()函数可以返回输入流中的下一个字符而不把这个字符从输入流中取出。
  2. 利用c中的getc和ungetc实现了c++中类似的功能。
  3. peek_nextchar()和get_nextchar()返回下一个非空的字符。
char cpeek() {
    char ans = getc(stdin);
    ungetc(ans, stdin);
    return ans;
}

char peek_nextchar() {
    char ans;

    do {
        ans = getc(stdin);
    } while(isspace(ans));
    ungetc(ans, stdin);

    return ans;
}

char get_nextchar() {
    char ans;

    do {
        ans = getc(stdin);
    } while(isspace(ans));

    return ans;
}

猜你喜欢

转载自blog.csdn.net/cfarmerreally/article/details/78474979