Advertisement
ProgramoBien

Word counter (C++)

Feb 1st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int check(string &s);
  8. int action=0;
  9.  
  10. int main()
  11. {
  12.     ifstream ckk;
  13.     ckk.open("t.txt");
  14.     string s;
  15.     int sum=0;
  16.     while(getline(ckk,s)){
  17.         for(int i=0;i<1;i++){
  18.             sum+=check(s);
  19.             if(sum%9000==0){
  20.                 cout<<sum;
  21.                 cout<<"\n"<<action;
  22.                 //cout<<system("CLS");
  23.                 //cout<<system("dir/s");
  24.                 cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
  25.             }
  26.         }
  27.     }
  28.     cout<<"SUM: "<<sum<<endl;
  29.     cout<<"Chars: "<<action<<endl;
  30. }
  31.  
  32. int check(string &s){
  33.     bool word=true;
  34.     int n=0;
  35.     bool endOfWord=true;
  36.     for(int i=0;i<s.size();i++){
  37.         action++;
  38.         if(s[i]!=' ')
  39.             word=true;
  40.         else word=false;
  41.         if(word){
  42.             if(i+1==s.size()){
  43.             endOfWord=true;
  44.             word=false;
  45.             }
  46.             else if(i+1<s.size() and s[i+1]==' '){
  47.             endOfWord=true;
  48.             word=false;
  49.             }
  50.         }
  51.         if(endOfWord){
  52.             ++n;
  53.             endOfWord=false;
  54.         }
  55.     }
  56.     if(s[0]!=' ')
  57.         n++;
  58.     if(s.size()>1)
  59.         if(s[0]!=' ' && s[1]!=' ')
  60.             n--;
  61.     return n-1;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement