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

#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.

 SAMPLE INPUT
this is a nice program

 SAMPLE OUTPUT
Words = 5
Characters = 18
 

Search site

Copyright © 2012 Dadaso Zanzane. All rights reserved.