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

C++ examples for Data Type:char

Description

Counts characters and words typed in

Demo Code

#include <iostream>
using namespace std;
#include <conio.h>            //for getche()
int main()/*w ww  .  j  a  va2 s .c  om*/
{
   int chcount=0;             //counts non-space characters
   int wdcount=1;             //counts spaces between words
   char ch = 'a';             //ensure it isn't '\r'
   cout << "Enter a phrase: ";
   while( ch != '\r' )        //loop until Enter typed
   {
      ch = getche();          //read one character
      if( ch==' ' )           //if it's a space
         wdcount++;              //count a word
      else
         chcount++;              //count a character
   }
   cout << "\nWords=" << wdcount << endl << "Letters=" << (chcount-1) << endl;
   return 0;
}

Result


Related Tutorials