题目:
编写一个程序,要求统计输入文本的行数。
Input
每行输入任意长度的字符串(每一行的字符串的长度小于等于1000),以输入仅由符号@构成的行作为结束, @所在的行不计入行数。
Output
输出文本的行数。
Sample Input
Hello world!
I come from China!
I’m a boy!
@
Sample Output
3
起初准备用单个字符的方法来解决问题发现,后来转到字符串。
详细代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{int j=0;char a[1000];int hn=0;gets(a);j=strlen(a);while(j!=1||a[0]!='@')//跳出循环的谈条件是j==1&&a[0]=='@',所以进入循环的条件即为前者的否定{hn++;gets(a);j=strlen(a);}printf("%d\n",hn);return 0;
}
要点:
1.注意好进入循环的条件,通过对首个字符和整个字符串的长度进行判断。
2.貌似判断"\n"是一个陷阱。
3.此题可能无法通过单个字符输入来判别行数