the c program1.8

来源:互联网 发布:农村淘宝服务站怎么赚钱 编辑:程序博客网 时间:2024/05/22 04:50

编写一个用于统计空格、制表符与换行符个数的程序

#include <iostream>using namespace std;int main(){int c;int blanks;int tabs;int newlines;blanks=0;tabs=0;newlines=0;while ((c=getchar()) != EOF){if (c == ' ') blanks++;if (c == '\t')tabs++;if (c == '\n')newlines++;}cout<<blanks<<endl;cout<<tabs<<endl;cout<<newlines<<endl;return 0;}//int main()//{//  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;//} 


0 0