Home
»
» Unlabelled
» C++ Program To Count Number Of Words In String
Count Words In String_
Source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| #include<iostream>
using namespace std;
int main( )
{
char str[80];
cout << "Enter a string: ";
cin.getline(str,80);
int words = 0; // Holds number of words
for(int i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ') //Checking for spaces
{
words++;
}
}
cout << "The number of words = " << words+1 << endl;
return 0;
}
|
Enter a String: You can do anything, but not everything.
The number of words = 7
Post a Comment