Count number of letters and words present in string

The following program takes a string as input and prints number of letters and number of words present in the string. The following code is implemented using  c.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    int c=0,c1=0,i;
    char str[30];
    printf("Enter a sentence : \n");
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {
        c++;
        if(str[i]==' ')
            c1++;

    }
    printf("\nNumber of letters present : %d",c);
    printf("\nNumber of words present : %d",c1+1);
    return 0;
}

Output:

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java