Mar 12, 2020

C Program 08

8.     Find the number of words in a sentence.

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
    char str[MAX_SIZE];
int i, words;
clrscr();
    /* Input string from user */
printf("\nEnter any string: ");
    gets(str);

i = 0;
    words = 1;

    /* Runs a loop till end of string */
    while(str[i] != '\0')
    {
        /* If the current character(str[i]) is white space */
        if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
        {
            words++;
        }

i++;
    }

printf("Total number of words = %d", words);
getch();
    return 0;
}





No comments:

Post a Comment