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 :

    1. Input the string.
    2. Find the number of letters including space.
    3. Find the number of words.
    4. Divide the number of letters by the number of words.
    5. 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

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

C PROGRAM TO PRINT THE REMAINING DAYS AND COMPLETED DAYS FROM A GIVEN DATE