Entri Populer

Selasa, 25 Januari 2011

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.

Tidak ada komentar:

Posting Komentar