Monday, May 5, 2008

program to count number of words in c++

Write a program that will count the number of words in a sentence entered by the user.

PROGRAM:

#include <iostream.h>

#include <conio.h>


void main() {

clrscr();

int wrdcnt=1;

char ch;

cout<<"Program to count number of WORDS entered by the user: \n";

cout<<"Enter your sentence: \n";

while((ch=getche())!='\r')

{

if(ch==' ')

wrdcnt++;

}

cout<<"\ntotal words in above sentence: "<<wrdcnt;

getch();

}

No comments: