Monday, 4 April 2016

C++ Program For Count A Words In Given String Even Enter No Of Space's Between String

#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;
main()
{
char str[100],a;
int w=0,l,c=0,j;
cout<<"\nPlease Enter A String: \n\n";
gets(str);
for(int i=0;i<100;i++)
    if(str[i]==' ')
    c++;
    else
        break;

for(j=c;str[j]!='\0';j++)
{
if(str[j]==' ' && str[j+1]!=' ')

w++;

}
if(str[j-1]==' ')
  w--;
cout<<w+1;
return 0;
}

Output:-

www.programmingwithbasics.com

No comments:
Write comments

Recommended Posts × +