Wednesday 18 December 2013

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);
    }
}

Output

 Welcome to C Class
 
The words  in  the sentence are:
Welcome
to
C
Class
 
Total number of words:  4 
 
 
 
No sentence given on  command  line

For More Details Please Visit Ictjobs.info

No comments: