检查段落的第一个字符是否为小写

时间:2015-06-05 15:05:54

标签: c file

我目前正在开展一项大学项目,我需要帮助,因为我脑子里还没有发生火花。

标题非常自我解释,我需要检查段落的第一个字符是否为小写,如果是,计算其中存在多少 total(这对检查txt文件中的错误很有用)

目前我有这段代码找到一个段落,然后它应该检查第一个字符是否是小写,问题是我不明白我怎么能指向段落后面的第一个字符,因此我无法输入if语句,因为new-line将再次为0。 printf("ola\n");只是为了检查我是否进入条件,他们在代码中没有其他目标。谢谢

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

int ch;
   FILE *fp;
   int paragraph=0;
   int newLine=0;
   int temp=0;
   fp = fopen("123.txt","r");

   while( ( ch = fgetc(fp) ) != EOF ){

        if(ch=='\n'){
            newLine++;
        }
        -


        if(newLine==2 && ch!='\n'){
            printf("ola\n");
            if(ch >= 'a' && ch <= 'z'){
                printf("ola\n");
                temp++;
                newLine=0;
            }
        }
    }   

    printf("%d\n",temp);
    fclose(fp);
    return 0;
    }

1 个答案:

答案 0 :(得分:2)

看过第一个字符后,必须重置换行符。您还需要确保看到2个或更多没有中间字符的换行符:

while( ( ch = fgetc(fp) ) != EOF ){

    while( ch=='\n' && (ch=fgetc(fp))!=EOF && ch=='\n'){
        newLine= 2;
    }


    if(newLine==2 && ch!='\n'){
        if(ch >= 'a' && ch <= 'z'){
            temp++;
        }
        newLine=0;
    }
}   
相关问题