新浪博客 发表时间 -- 2009-07-27 18:28:54
题目:
Input
The input file will contain an unknown number of lines with at most 100 characters on each line. All the characters will be printable ASCII characters. Note that the input file will always exist, but may be empty.
Output
The number of lines in the input file.
Sample Input
one and two three
Sample Output
3
算法:
一看道这题,就知道要设计数器来求它的行数了,一开始设一个数组来接收输入的每一行,然后让计数器count=0,用d=strilen()来求出数组长度,当它到每行末尾时,也就是数组的长度为d时它的值为'\0'就让count++。。。。
代码:
#include "stdio.h" #include "string" int main() {char str[200]; int d,count=0,i; freopen("e:\\z1010.txt","r",stdin);//读文件的 while (gets(str)) { d=strlen(str); for (i=0;i<=d;i++) { if (str[i]=='\0') { count++; } } } printf("%d\n",count); return 0; }