C program to count number of words in a sentence or string - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, January 14, 2020

C program to count number of words in a sentence or string - My CS Tutorial

C program to count number of words in a sentence or string.
In this tutorial we will learn how to write a c program to count or find the number of words in a string or sentence.
we will enter a string or sentence then it will print the number of words of that string.


C program code


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,word=1;
    char str[100];
    clrscr();
    printf("\n ENTER A STRING TO FIND THE NUMBERS OF WORDS:\n ");
    gets(str);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==' ')
        {
            word++;
        }
    }
    
    printf("\n NUMBER OF WORDS IN THIS STRING IS:\n ");
    printf("%d\n",word);
    getch();
}


OUTPUT:




We can see that we will enter a string or sentence then it will print the number of words of that string.

Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment