编写一个统计空格,制表符与换行符个数的程序
- #include<stdio.h>
- main()
- {
- int sp,tab,lf,c;
- sp=0;
- tab=0;
- lf=0;
- while((c=getchar())!='0')
- {
- if (c==' ')
- ++sp;
- else if (c=='/t')
- ++tab;
- else if (c=='/n')
- ++lf;
- }
- printf("space is %d/n",sp);
- printf("tab is %d/n",tab);
- printf("line feed is %d/n",lf);
- }
第一题比较简单
答案:
#include <stdio.h>
int main(void)
{
int blanks, tabs, newlines;
int c;
int done = 0;
int lastchar = 0;
blanks = 0;
tabs = 0;
newlines = 0;
while(done == 0)
{
c = getchar();
if(c == ' ')
++blanks;
if(c == '/t')
++tabs;
if(c == '/n')
++newlines;
if(c == EOF)
{
if(lastchar != '/n')
{
++newlines; /* this is a bit of a semantic stretch, but it copes
* with implementations where a text file might not
* end with a newline. Thanks to Jim Stad for pointing
* this out.
*/
}
done = 1;
}
lastchar = c;
}
printf("Blanks: %d/nTabs: %d/nLines: %d/n", blanks, tabs, newlines);
return 0;
}