C Program to Calculate the number of characters,words and the lines in the given text..

#include<stdio.h>
int noofchar(char *s);
int noofwords(char *s);
int nooflines(char *s);

void main()
{
    char s[100];
    printf("Enter the text with terminator ~\n");
    scanf("%[^~]",&s);
    int c,w,l;
    c=noofchar(s);
    w=noofwords(s);
    l=nooflines(s);
    printf("\nThe no of characters in the given text is ..%d",c);
    printf("\nThe no of words in the given text is ..%d",w);
    printf("\nThe no of lines in the given text is ..%d",l);
}

int noofchar(char *s)
{
    int i=0,c=0;
    while(s[i]!='\0')
    {
        if(s[i]!=' '&&s[i]!='\n')
            c++;
        i++;
    }
    return c;
}

int noofwords(char *s)
{
    int c=0,i=0;
    while(s[i]!='\0')
    {
        if((s[i]!=' '&&s[i]!='\n')&&s[i+1]==' ')
            c++;
        if((s[i]!=' '&&s[i]!='\n')&&s[i+1]=='\n')
            c++;
        i++;
    }
    return c+1;
}

int nooflines(char *s)
{
    int c=0,i=0;
    while(s[i]!='\0')
    {
        if(s[i]=='\n')
            c++;
        i++;
    }
    return c+1;
}

No comments:

Post a Comment