计算文本中的行数,字符串数以及字符个数
#include <stdio.h>
#include <string.h>
main()
{
char line[100],ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf("Key in the text.\nGive one space after each word.\n");
printf("When completed,press 'Return'.^_^\n\n");
while (end == 0)
{
//reading a line of text.
c = 0;
while((ctr=getchar())!='\n')
line[c++]=ctr;
line[c]='\0';
//counting the words in a line.
if (line[0]=='\0')
{
break;
}
else
{
words++;
for(i=0;line[i]!='\0';i++)
if(line[i] == ' '|| line[i] == '\t')
words++;
}
//counting lines and characters.
lines++;
characters = characters + strlen(line);
}
printf("\nNumber of lines = %d\n",lines);
printf("Number of words = %d\n",words);
printf("Number of characters = %d\n",characters);
}