Computer Scientists are Pretty Pessimistic

Friday 4 September 2015

WAP to count the number of spaces, tabs and new line characters in a given string.

#include <stdio.h>
int main ()
{
    char ch[100];
    int s=0,t=0,nl=0,i;
    printf("Enter String: ");
    gets(ch);
    for(i=0;i<ch[i];i++)
    {
        if(ch[i]==' ')
            s++;
        else if(ch[i]=='\t')
            t++;
        else if(ch[i]=='\n')
            nl++;

    }
    printf("Spaces : %d\nTabs : %d\nNew line : %d",s,t,nl);
    return 0;
}

No comments:

Post a Comment