Computer Scientists are Pretty Pessimistic

Thursday 1 October 2015

WAP to count the number of words and number of characters in a given line of text except the spaces.

CODE ::

#include <stdio.h>
int main ()
{
    int w=1,l=0,i;
    char c[100];
    printf("Enter string: ");
    gets(c);
    for(i=0;i<c[i];i++)
    {
        if(c[i]>='a'&&c[i]<='z'||c[i]>='A'&&c[i]<='Z') //count letter
            l++;

        if(c[i]==' '||c[i]=='.'||c[i]==','||c[i]=='?'||c[i]=='!'||c[i]==';'||c[i]==':')
        {
            if(c[i+1]>='a'&&c[i+1]<='z'||c[i+1]>='A'&&c[i+1]<='Z')
                w++;
        }
    }
    printf("Words: %d\nLetters: %d",w,l);
    return 0;
}

No comments:

Post a Comment