codescracker


c

C Program to Count Occurrence of Words in Sentence



« Previous Program Next Program »


Count Occurrence of Words in Sentence in C

To count the occurrence of words in a string or sentence in C programming, you have to ask to the user to enter the sentence and start counting all words present in the sentence and display the value on the screen.

C Programming Code to Count Occurrence of Words in Sentence

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:

/* C Program - Count Occurrence of Words in Sentence */
		
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
	clrscr();
	char strs[100], countw=0, strw[15], i;
	printf("Write a sentence : ");
	gets(strs);
	int len=strlen(strs);
	for(i=0; i<len; i++)
	{
		if(strs[i]==' ')
		{
			countw++;
		}
	}
	printf("Total number of words in the sentence is %d",countw+1);
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program count number of words in sentence

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test