- Home
- Course Category
- Course Result
- Course Detail
Hot Line: 0124-2383121
Course Introduction
Programs for practice
Following C++ program ask to the user to enter/write a sentence, to count the total number of words in the sentence, then display the result on the screen. This is a simple program which will count the words of simple sentence:
Source Code: Print Hello World
/* C++ Program - Count Word in Sentence */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char strs[100], countw=0, strw[15];
int i, len;
cout<<"Write a sentence : ";
gets(strs);
len=strlen(strs);
for(i=0; i<len; i++)
{
if(strs[i]==' ')
{
countw++;
}
}
cout<<"Total number of words in the sentence is "<<countw+1;
getch();
}
Output
Write a sentence :welcome to blazingminds
Total number of words in the sentence is 3