Q:

C Program to Count Number of Words in a given string

0

C Program to Count Number of Words in a given string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this example code, I am counting the word in a given string using the above-described function followed by described steps. You can see the string”Welcome to aticleworld”, it contains three words.

#include <stdio.h>
#define TRUE   0
#define FALSE  1
unsigned wordCounter(char *PString)
{
    int flag = TRUE;
    unsigned int wCounter = 0; // word counter
    // Run until not get null character
    while (*PString)
    {
        //Set the flag true if you got the space
        if (*PString == ' ')
        {
            flag = TRUE;
        }
        else if (flag == TRUE) //if next word is not empty and flag is true,
        {
            //increment word counter
            flag = FALSE;
            ++wCounter;
        }
        // Move to next character
        ++PString;
    }
    return wCounter;
}
int main(void)
{
    char *pMsg = "Welcome to aticleworld"; //string
    unsigned int count = 0;
    count = wordCounter(pMsg); //function count the words
    printf("No of words : %u",count);
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Count Number of Words in a given file... >>
<< Are the expressions *++ptr and ++*ptr same?...