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
*/
#include <stdio.h>
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");
return;
}
else
{
printf("\nThe words in the sentence are:");
/*
* From argv[1] to argv[argc -1] calculate the number of arguments
*/
for (i = 1;i < argc;i++)
{
printf("\n%s", argv[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.
advertisement
advertisement
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.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice BCA MCQs
- Check Computer Science Books
- Practice Computer Science MCQs
- Check C Books
- Watch Advanced C Programming Videos