Sample – Program to count the number of words and characters in a sentence.

by Nideesh C on February 8, 2011 · 0 comments

in C++




#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int countch=0;
int countwd=1;
cout << “Enter your sentence in lowercase: ” << endl;
char ch=’a';
while(ch!=’\r’)
{
ch=getche();
if(ch==’ ‘)
countwd++;
else
countch++;
}
cout << “\n Words = ” << countwd << endl;
cout << “Characters = ” << countch-1 << endl;
getch();
}

This program takes in a sentence as a screen input from the user.
It then determines the number of words and characters in the sentence using the ‘WHILE’ loop and outputs them using the ‘cout’ command.

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – count number of characters and lines in a file
  2. Sample – Program to enter a string and find its length.
  3. Sample – check if a number is prime
  4. Sample – understanding life time and scope
  5. Sample – understanding scope

Leave a Comment

Previous post:

Next post: