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;
}
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.
need an explanation for this answer? contact us directly to get an explanation for this answer