Advertisement
saira12tabassum19

count number of words

Aug 13th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int count_word(string s)
  5. {
  6. int i,count=0,len;
  7. len=s.size();
  8. for(i=0;i<len-2;i++)
  9. {
  10.  
  11.     if((s[i]==' '&&s[i+1]>='a'&&s[i+1]<='z'&&s[i+2]>='a'&&s[i+2]<='z')||(s[i]==' '&&s[i+1]>='A'&&s[i+1]<='Z'&&s[i+2]>='A'&&s[i+2]<='Z'))
  12.         count++;
  13. }
  14. return count;
  15. }
  16. int main ()
  17.  
  18. {
  19.  
  20.    string s;
  21.     cout << "Enter a string : ";
  22.     getline(cin,s);
  23.     int count_w=count_word(s);
  24. if(s[0]==' ')cout<<count_w<<endl;
  25. else
  26.  cout<<count_w+1<<endl;
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement