Count characters and words typed in - C++ Data Type

C++ examples for Data Type:char

Description

Count characters and words typed in

Demo Code

#include <iostream>
using namespace std;
#include <conio.h>            // for getche()
int main()/*  w w w. jav  a  2 s  . c om*/
{
   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;
}

Result


Related Tutorials