Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- int count_word(string s)
- {
- int i,count=0,len;
- len=s.size();
- for(i=0;i<len-2;i++)
- {
- 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'))
- count++;
- }
- return count;
- }
- int main ()
- {
- string s;
- cout << "Enter a string : ";
- getline(cin,s);
- int count_w=count_word(s);
- if(s[0]==' ')cout<<count_w<<endl;
- else
- cout<<count_w+1<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement