C++ char type Count characters and words typed in

Description

C++ char type Count characters and words typed in

#include <iostream>
using namespace std;
#include <conio.h>            // for getche()
int main()//from  w  w w .ja v  a  2s . c o  m
{
   int chcount=0;
   int wdcount=1;             // space between two words
   char ch;
   while( (ch=getche()) != '\r' )  // loop until Enter typed
   {
      if( ch==' ' )           // if it's a space
         wdcount++;           // count a word
      else
         chcount++;           // count a character
   }                       // display results
   cout << "\nWords=" << wdcount << endl << "Letters=" << chcount << endl;
   return 0;
}



PreviousNext

Related