C program to count the total number of words in a string – In this article, we will detail in on the several means to count the total number of words in a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using Pointers and While Loop
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As given in the image above, firstly, you need to enter a string.
The string specified here is as follows:
“always first never give up”
As you can see, there are 5 words in the given string.
It can be found out by basic reading itself.
Hence, doing the same in C programming is as follows:
Using Standard Method
- Read the entered string and initialize to s using gets(s).
2) We are finding the words count based on white spaces present in the given string. The ASCII value of white space is 32.
3) for loop iterates through the string with the structure for(i=0;s[i];i++),
If ASCII value of any character of a string is equal to ASCII value of white space i.e 32, then increase the word count.
4) After all iterations of for loop increase the word count, if i>0.
5) Print the number of words present in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int i,words=0; printf("Enter the string : "); gets(s); for(i=0;s[i];i++) { if(s[i]==32) words++; } if(i>0) words++; printf("no of words in string = %d\n",words); return 0; } |
Output:
1 2 |
Enter the string: welcome to Cbeginners no of words in string = 3 |
Using Function
- The main() function calls the stringwordcount(char *s) function, passing the string as an argument to the function.
2) The function stringwordcount() function compares each character’s ASCII value with white space ASCII value 32.If any character is equal to white space, then it increases the word count.
3) After all iterations of for loop, if i>0 then increase the word count by 1.
4) The function returns the word count to main() function. The main() function prints the number of words present in the given string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> #include <string.h> int stringwordcount(char *s) { int i,words=0; for(i=0;s[i];i++) { if(s[i]==32) words++; } if(i>0) words++; return words; } int main() { char s[1000]; int wordscount; printf("Enter the string: "); gets(s); wordscount=stringwordcount(s); printf("no of words in string = %d\n",wordscount); } |
Output:
1 2 |
Enter the string: always first never give up no of words in string = 5 |
Using Recursion
- The main() calls the function stringwordcount(char *s).
2) The function counts the number of words as
a) If the s[i] is null, then it increases the word count by 1 if i>0 and returns the word count value to the main() function.
b) If s[i] is not null, then compare ASCII value of s[i] with 32 which is the ASCII value of white space. If s[i] is equal to white space then increase the word count and call the function stringwordcount().
The function calls itself recursively until s[i] becomes to null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <string.h> int stringwordcount(char *s) { static int i,words=0; if(!s[i]) { if(i>0) words++; return words; } else { if(s[i++]==32) words++; stringwordcount(s); } } int main() { char s[1000]; int wordscount; printf("Enter the string: "); gets(s); wordscount=stringwordcount(s); printf("no of words in string = %d\n",wordscount); } |
Output:
1 2 |
Enter the string: Without music, life would be a mistake no of words in string = 7 |
Using Pointers And While Loop
- The pointer variable p points the string s.
2) The while loop iterates until the character at the pointer variable become null.
a) If the ASCII value of the character at the pointer variable p is equal to white space ASCII value. Then increase the word count.
3) Increase the word count by 1 if the length of the string is greater than zero.
4) Print the number of words present in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <string.h> int main() { char s[1000],*p; int words=0; printf("Enter the string : "); gets(s); p=s; while(*p) { if(*p++==32) words++; } if(strlen(s)>0) words++; printf("no of words in string = %d\n",words); return 0; } |
Output:
1 2 |
Enter the string: I have not failed. I've just found 10,000 ways that won't work no of words in string = 12 |