我尝试编写一个程序来计算从硬盘驱动器中存在的文件中获取的文本中的单词和字母。但是这个程序只计算第一行的字母数。我该怎么办?请给出你对这个程序的想法。请在您的编译器中调试我的代码并帮助我改进它。
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int letter(const char sentence[ ]);
int words(const char sentence[ ]);
int main(int argc, char *argv[]) {
char sentence[100];
const char *filename = "test.txt";
FILE *cfPtr;
if (argc == 2)
filename = argv[1];
if ( (cfPtr = fopen(filename, "r")) == NULL ) {
printf( "File '%s' could not be opened\n", filename );
}
else {
int total = 0;
int total2 =0;
while (fgets(sentence, sizeof sentence, cfPtr))
total += words(sentence);
total2 +=letter(sentence);
printf("%d\n", total2);
printf("%d\n", total);
printf("****%s",*cfPtr);
fclose(cfPtr);
}
getch();
return 0;
}
int words(const char sentence[ ])
{
int i, length=0, count=0, last=0;
length= strlen(sentence);
for (i=0; i<length; i++)
{
if (sentence[i] == ' '||sentence[i] == '\t'||sentence[i] == '\n')
count++;
}
return count;
}
int letter(const char sentence[ ])
{
int i, length=0, count=0;
length= strlen(sentence);
for (i=0; i<length; i++)
{
if ((sentence[i]>'a'&&sentence[i]>'z')||(sentence[i]>'A'&&sentence[i]>'Z'))
count++;
}
return count;
}