String Counting

Submitted by Karthikeyan on Tue, 08/09/2009 - 17:32

Sample C Program to counter number of words and lines


#include
#include
#include
char c;
main()
{
	char c, str[200];
	int i =0;
	clrscr();
    printf("\n\t\t String counting");
    printf("\n\t\t ***************");
    printf("\n Enter a String and terminate it with * star\n");
    do
    {
        c=getchar();
        str[i++]=c;
    }
    while(c!='*');
    count(str);
}
count(char *s)
{
    int l=1, w=1;
    printf("\n The given strings: \n");
    while(*s != '*')
    {
        printf("%c", *s);
        if(*s == ' ')
        w++;
        if(*s == '\n')
        {
            if(c != ' ')
            w++;
            c = *s;
            l++;
        }
        s++;
    }
    if(w ==0 && l ==0)
    {
        printf("\n There is one word and one line");
        exit();
    }
    if(l ==0)
    l=1;
    printf("\n There are %d words and %d lines", w,l);
    printf("\n\t *****************************");
    getch();
}