This C Program counts total number of words in the sentence using command line argument.
Here is source code of the C Program to Count the total number of words in the sentence using command line argument. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Count the Total Number of Words in the Sentence
* using Command Line Argument
int main(int argc, char *argv)
int i = 0;
/* If no sentence is given in the command line */
if (argc == 1)
printf("\n No sentence given on command line");
printf("\nThe words in the sentence are:");
* From argv to argv[argc -1] calculate the number of arguments
for (i = 1;i < argc;i++)
printf("\n\nTotal number of words:");
printf(" %d", argc-1);
$ gcc arg1.c $ a.out Welcome to C Class The words in the sentence are: Welcome to C Class Total number of words: 4 a.out No sentence given on command line
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
- 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
- Practice BCA MCQs
- Apply for Computer Science Internship
- Buy C Books
- Watch Advanced C Programming Videos
- Buy Computer Science Books