This is a C Program to Count the Number of Words in a given text or Sentence.
This program takes a string as input and count the number of words in the input string.
1. Take a string as input.
2. Using for loop search for a empty space in between the words in the string.
3. Consecutively increment a variable. This variable gives the count of number of words.
Here is source code of the C Program to Count the Number of Words in a given text Or Sentence. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Count Number of Words in a given Text Or Sentence
int count = 0, i;
printf("Enter the string:\n");
for (i = 0;s[i] != '\0';i++)
if (s[i] == ' ' && s[i+1] != ' ')
printf("Number of words in given string are: %d\n", count + 1);
1. Take a string as input and store it in the array s.
2. Using for loop search for a space ‘ ‘ in the string and consecutively increment a variable count.
3. Do step-2 until the end of the string.
4. Increment the variable count by 1 and then print the variable count as output.
Enter the string: welcome to sanfoundry's c-programming class! Number of words in given string are: 5 Enter the string: Best Books in C Programming Number of words in given string are: 6
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy Computer Science Books
- Watch Advanced C Programming Videos
- Buy C Books
- Practice BCA MCQs
- Apply for Computer Science Internship