Question 4: Write a C++ program to find number of words in the string. 

February 13, 2019
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:

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.