C Program to Count Number of Words in a String

Write a C program to count number of words in a string. Given an input string, we have to write a code to count number of words in a string. The words in a string are separated by space(‘ ‘).

For example –

Example 1

Input String – I love webrewrite dot com
Output – 5

Example 2

Input String – I love webrewrite               dot com
Output – 5

C Program to Count Number of Words in a String

C Program to Count Number of Words in a String

C Objective questions

Subscribe Our Tutorials

Get Latest Updates on Facebook

Question on Linked List

Logic to Count Number of Words in a String

Traverse a string and check if a current character is not space and next character is space then increment the count by 1.

Let’s implement them through C code.

C Program to Count Number of Words in a String

 

C programming questions for practice

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.