Input can be "a39*^$ghdG#(@"
What is the easiest way to code a program that counts letters, numbers, and other characters seperatly. Then have and output of
number of letters =
number of numbers =
number of other characters =
hi, i'm mo3taz,egypt
i've coded this program but it counts the number of characters
you can change the sellection statements to make it count the number of numbers
this is the code
#include <iostream.h>
# include <conio.h>
char previousCH, ch ,nextchar ;
int nonAlphabet = 0 , //characters like ',' '.' ' ' ''' ......
wordnum = 0 , // number of words
statenum = 0 , // number of statements (a statement ends with (.))
longword = 0 , // the long word is more than 10 characters
alphabet = 0 , // alphabetical characters
longtemp = 0 , // used to count long words
spacenum = 0 ; // number of spaces
/* function checks for next alphabetic characters */
bool charcheck (char c)
{
if ((c >='a' && c <= 'z') || ((c >= 'A') && (c < 'Z')))
return 1;
elsereturn 0;
}
//...............................
int main()
{
cout << "please enter a text paragraph ended with '$' character:\n\n";
while(nextchar != '$' ) // "$" is the end of file or input
{
nextchar = cin.peek();
if (nextchar == '\n') // ignore or eat new line character
cin.ignore(1,'\n');
previousCH = ch; // this character will be previous one the next loop
cin.get(ch);
if (charcheck(ch))
{
alphabet++;
longtemp++;
if (longtemp == 10) // count long words
{
longtemp = 0;
longword++;
}
}
else
{
nonAlphabet++;
//.... don't count a word if you found a space unless it was preceeded or followed with characters
if ((ch== ',') || (ch == '.') || (ch == ' ' && (charcheck(cin.peek())) && (charcheck(previousCH))))
{
wordnum++;
longtemp = 0;
if (ch == ' ') spacenum++;
}
if (ch == '.')
statenum++;
}
}
nonAlphabet--; // ignore '$' character
wordnum++;
cout << "the data of this text document is:\n";
cout << "______________________________________________________________\n";
cout << "\nthe number of alphabetic characters is \t:" << alphabet << endl;
cout << "\nthe number of nonalphabetic characters \n"
<<"with spaces is ..........................\t:" << nonAlphabet << endl;
cout <<"\nwithout spaces ..........................\t:" << (nonAlphabet - spacenum) << endl;
cout << "\nthe number of words is ..................\t:" << wordnum << endl;
cout << "\nthe number of statements is .............\t:" << statenum << endl;
cout << "\nthe number of long words is .............\t:" << longword<< endl;
getch();
}
example input:
word word word,word,word word, word. word word
word word,word,word.word$
hint: the end of text is $
itdoesn't matter to add spaces or not with "," - "." - "$"