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 '$'.
Print count of characters, count of digits and count of white spaces respectively (separated by space).
Input Format :
Output Format :
Sample Input :
Sample Output :
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;
}
0 comments:
Post a Comment