BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan

BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan (http://bzupages.com/)
-   Object Oriented Programming (http://bzupages.com/300-object-oriented-programming/)
-   -   Program to count the number of words and characters in a sentence (http://bzupages.com/f300/program-count-number-words-characters-sentence-14654/)

bonfire 08-03-2011 11:54 PM

Program to count the number of words and characters in a sentence
 
1 Attachment(s)
Program to count the number of words and characters in a sentence


#include
#include

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


All times are GMT +5. The time now is 02:52 PM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.