Question 4: Write a C++ program to find number of words in the string.
Question 4: Write a C++ program to find number of words in the string.
/*
Name: Mudassar Ali
Roll: BCSF15M045 [Morning]
Assignment: No# 05
Teacher: Miss Sadia Shahzad
*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"This programme will count the number of words from string!!\n\n";
char str[50]; // To hold the content of string.
cout<<"Enter the string: ";
cin.getline(str,50); // Inputting the string with spaces.
int count = 1; // count the words with the help of number of spaces.
for(int x = 0; str[x]!='\0';x++)
{
if(str[x]== ' ') // If spaces occur in the string then count++ will perform his action
{
count ++;
}
}
cout<<"\nNumber of words = "<<count<<endl; //Printing number of words in the string.
return 0;
}
No comments: