Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 34: Write a C++ program to count the word “to” in a string.

#include<iostream>
#include<string.h>
using namespace std;

int main()
{
	int count=0,length=0;
	char str[100];
	cout<<"\nenter a string : ";
	gets(str);

	for(int i=0;str[i]!='\0';i++)
	{
		if(str[i]=='t'&&str[i+1]=='o'&&str[i+2]==' '&&i==0)
		{
			count++;
			i++;
		}
		else if(str[i]==' '&&str[i+1]=='t'&&str[i+2]=='o'&&(str[i+3]==' '||str[i+3]=='\0'))
		{
			count++;
			i+=2;
		}
	}
	cout<<"\ntotal no of 'to' in a string : "<<count;
	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.