C Program to Count Number of Words in a given Text Or Sentence

This C Program Counts the Number of Words in a given text Or Sentence.

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.

  1. /*
  2.  * C Program to Count Number of Words in a given Text Or Sentence
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. void main()
  8. {
  9.     char s[200];
  10.     int count = 0, i;
  11.  
  12.     printf("enter the string\n");
  13.     scanf("%[^\n]s", s);
  14.     for (i = 0;s[i] != '\0';i++)
  15.     {
  16.         if (s[i] == ' ')
  17.             count++;    
  18.     }
  19.     printf("number of words in given string are: %d\n", count + 1);
  20. }


$ cc string2.c
$ a.out
enter the string
welcome to sanfoundry's c-programming class!
number of words in given string are: 5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Follow Manish & Sanfoundry