Tuesday, 20 October 2015

C program to print total number of words in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20],ch=0;
int i=0,wcnt=1;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
                if(ch==' ')
                      wcnt++;
                putchar(ch);
                i++;

        }
printf("\n total no. of words in string=%d",wcnt);
getch();
}

No comments:

Post a Comment