# C PROGRAMS
String in C
➤ Read Display String in C
➤ 2D Array of Strings in C
➤ Find Length of String
➤ Copy Two Strings in C
➤ Concatenate Two Strings
➤ Compare two strings
➤ Reverse a String in C
➤ Palindrome string in C
➤ Uppercase to Lowercase
➤ Lowercase to Uppercase
➤ Remove all characters in String except alphabet
➤ Find Frequency of Characters
➤ Count Number of Words
➤ Count lines, words, & characters
➤ Count Vowel, Consonant, Digit, Space, Special Character
➤ String Pattern in C
➤ Copy Input to Output
Write a C program to count number of words in a string. Take string as input from the user.
Sample of input/Output:-
Enter a string: Know Program
Word: 2
Count Number of Words in a String
C program to count the number of words in a string, In this program string, is taken the user. Now we check every character of the string. You may think that why so many if statements are considered for this simple program. We will discuss all those one by one.
#include<stdio.h>
int main()
{
char s[150];
int i, word=1;
printf("Enter a string: ");
scanf("%[^\n]",s);
for(i = 0; s[i] != '\0'; ++i)
{
if(s[i]==' '&& s[i+1]!=' ') word++;
}
if(s[i-1]==' ')word--;
if(s[0]==' ')word--;
if(s[0]=='\0')word--;
printf("Word: %d",word);
return 0;
}
Case1
if(s[i]==' '&& s[i+1]!=' ') word++;
Why we use this if condition. To understand it, replace below line with the previous line, and given below input and observe the output. In input there is many spaces between words.
if(s[i]==' ') word++;

Is there actually 10 words in the string? No, this output came because we only considered that, if there is a space after any character then it is a word. But it is the wrong approach, the user may enter any string.
If there is a space after any character then word variable will increases but if the character after space is also space then word variable will not increase. That’s why we should also check the character after space.
Case2
if(s[i-1]==' ') word--;
Why this line? Remove this line from the program and test with below-given input (There are many spaces in the input after the “Know Program” and before the enter key).

It is an incorrect result. We got this result because in the string, at last of the string there is a space and ‘\0’. Here case1 condition becomes true because after space there is a character which is not space, so the variable word increases. To solve this issue we need to add the above if condition.
Case3
if(s[0]==' ') word--;
Remove this line and run your program with given below input (In the below input there is much space before the string).

We got an incorrect result, it also violates the case1. Before “Know Program”, there is a space and then there is a keyword ‘K’ due to this, the variable word increases. To solve this issue, above if condition should be added in the program.
Case4
if(s[0]=='\0') word--;
Enter a string:
Word: 1
There is a chance that the without pressing any character the user may press enter key. So, it will also be treated as a word. But actually it is not a word. So, also add this if condition to the program for the correct result.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Hi, I desire to subscribe for this blog to get latestupdates, thus where can i do it please help out.