counting characters

Feb 22, 2008 at 6:40pm
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 =
Feb 22, 2008 at 7:36pm
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#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;
 else
 return 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 "," - "." - "$"

you are welcome

Last edited on Feb 22, 2008 at 7:40pm
Topic archived. No new replies allowed.