Write a C program to count total words in text

Aim:- Write a C program to count total words in text.

C Program Code:

 
#include<stdio.h>
#include<conio.h>
void main()
{
    char h[16], ch;
    int i, c = 0;
    clrscr();
    printf("Enter string : ");
    for(i = 0; ch != '\n'; i++)
    {
        ch = getchar();
        h[i] = ch;
    }
    h[i] = '\0';
    for(i = 0; h[i] != '\0'; i++)
    {

        if(h[i] == ' ')
        {
            c++;
            while(h[i] == ' ')
            i++;
        }

    }

    printf("\nTotal words : %d", c + 1);
    getch();

}       
            

Output:

Post a Comment

0 Comments