Sunday, February 12, 2017

Count Words in C

In this article I would like to share with you a sample program that will ask you to give a string and then our program will count the number of words in the given by the user using C as our programming language. The code is very short and easy to understand. Thank you.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing
 
word.c

#include <stdio.h>
#include <string.h>

#define NIL ' '

int main()
{

    char str[200];
    int count=0,a=0;

    printf("\t COUNT WORDS IN C PROGRAM");
    printf("\n\n");
    printf("\t Written By: Mr. Jake R. Pomperada, MAED-IT");
    printf("\n\n");
    printf("Give a String : ");
    scanf("%[^\n]s",&str);
     for (a=0;str[a] !='\0'; a++)
     {
        if (str[a]==NIL)
            {
              count++;
            }
     }
     printf("\n\n");
     printf("\t Display Results ");
     printf("\n\n");
     printf("The number of words in the given string is %d.",count+1);
     printf("\n\n");
     printf("\t End of Program ");
     printf("\n\n");
     return 0;
}

No comments:

Post a Comment