C PROGRAM TO FIND THE AVERAGE WORD LENGTH OF A SENTENCE
Consider the following I/O samples.
SAMPLE INPUT: Tech Trends Tract // 15 letters and 3 words
SAMPLE OUTPUT: 5.0 // 15 / 3 = 5.0 in float
The algorithm used here is :
- Input the string.
- Find the number of letters including space.
- Find the number of words.
- Divide the number of letters by the number of words.
- Print the output.
C PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int letter=0,word=0,i;
float len;
printf("Enter the String:");
scanf("%[^\n]s",&str);
for(i=0;i<strlen(str);i++) // WORD COUNT
{
if(str[i]!=' ')
word++;
}
for(i=0;str[i]!='\0';i++) // LETTER COUNT
{
if(str[i] == ' ' && str[i+1] != ' ')
letter++;
}
len = (float)(word)/(float)(letter+1);
printf("Average Word Length of a Sentence is : %.1f",len);
return 0;
}
OUTPUT:
The given source code in C program "to find the average word length of a sentence" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment