count characters

 Count Characters

Write a program to count and print the total number of characters (lowercase english alphabets only), digits (0 to 9) and white spaces (single space, tab i.e. '\t' and newline i.e. '\n') entered till '$'.

That is, input will be a stream of characters and you need to consider all the characters which are entered till '$'.

Input Format :

A stream of characters terminated by '$'

Output Format :

3 integers i.e. count_of_characters count_of_digits count_of_whitespaces (separated by space)

Sample Input :

abc def4 5$

Sample Output :

6 2 2

Sample Output Explanation :

Number of characters : 6 (a, b, c, d, e, f)

Number of digits : 2 (4, 5)

Number of white spaces : 2 (one space after abc and one newline after 4)


C++ Code:

#include <iostream>
using namespace std;

int main()
{
  int lc=0, digits=0, ws=0;
  char c = cin.get();
  while(c!='$')
  {
    if('a'<=c && c<='z')
    {
      lc++;
    }
    else if('0'<=c && c<='9')
    {
      digits++;
    }
    else if(c==' ' || c=='\t' || c=='\n')
    {
      ws++;
    }
    c = cin.get();
  }
  cout << lc << ' ' << digits << ' ' << ws << endl;
}




SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment