Saturday, August 20, 2016

Word Count in C++

Here is a sample program that I wrote in C++ that will count the number of words in a given sentence.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
 
My mobile number here in the Philippines is 09173084360.




Program Listing


#include<iostream>
#include<string>


using namespace std;

char str1[150],tmp;
int i=0,j=0,w=0,l=0;


void CntWords(char str1[150])

{

l=strlen(str1);

for (i=1;i<=l;i++)

 {


  if (str1[i]==' '&&str1[i+1]!=' ')
  {
  w++;


  }else{
  if (str1[i]!=' '&&str1[l]!=' '&&l==i)
  {
  w++;

  }


  }


 }

  if (str1[1]==' ' && str1[2]==' ')
   {

   w--;
   }

 tmp=str1[l-1];

 if (tmp==' ')
 {
 w--;

 }
}


 main()
{


cout << "\t\t  Word Count Version 1.0 ";
cout << "\n\n";
cout << "Type Words :=>  ";
gets(str1);

CntWords(str1);

cout << "\nThe Counted Words " << w;
cout << "\n\n";
system("PAUSE");


}





 

No comments:

Post a Comment