
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C program to calculate the average word length of a sentence using while loop
Problem
Enter a sentence at run time and write a code for calculating the average length of words that are present in a sentence
Solution
Algorithm
START Step 1: declare character, int and double variables Step 2: Enter any statement Step 3: while loop Check condition stmt[i]=getchar()) != '
' True then enter into loop Increment I and call the function at step 5 Step 4: Print the average length return by function From step 5 Step 5: called function calculatewordlength i. declare and initialize charcount=0 and wordcount=1 ii. while loop check condition (*stmt != '
') if it trues enter into loop 1. if(*stmt != ' ') 2. charcount++; 3. else if(*stmt == ' ') 4. wordcount++; 5. stmt++; iii. return (double)charcount/wordcount; STOP
Program
#include<stdio.h> #include<string.h> double calculatewordlength(const char *stmt); int main(){ char stmt[100]; int i=0; double avglen; printf("enter any statement:"); while((stmt[i]=getchar()) != '
') i++; stmt[i]='
'; avglen=calculatewordlength(stmt); printf("average length of word is:%f.
", avglen); } double calculatewordlength(const char *stmt){ int charcount=0; int wordcount=1; while(*stmt != '
'){ if(*stmt != ' ') charcount++; else if(*stmt == ' ') wordcount++; stmt++; } return (double)charcount/wordcount; }
Output
enter any statement:Tutorials Point is the best resource for online education average length of word: 5.444444444.
- Related Articles
- C++ program for length of the longest word in a sentence
- Java program to calculate the factorial of a given number using while loop
- Write a C program to reduce any fraction to least terms using while loop
- PHP program to find the first word of a sentence
- C++ Program to Calculate Average of Numbers Using Arrays
- Write a C# program to calculate a factorial using recursion
- Finding the length of second last word in a sentence in JavaScript
- Print longest palindrome word in a sentence in C Program
- Java program to reverse each word in a sentence
- Python program to reverse each word in a sentence?
- C program to find palindrome number by using while loop
- How to write a while loop in a JSP page?
- C++ program to Reverse a Sentence Using Recursion
- Java program to print the fibonacci series of a given number using while loop
- Python Program to replace a word with asterisks in a sentence

Advertisements