Counting characters in a string

I need help writing a program that counts the number of letters, numbers, and special characters independently while only using iostream. Here is what I have so far. I have only been able to get it to count number and letters but i am not quite sure how to go about counting special characters.

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
#include <iostream>
using namespace std;
int main()
{
char s[50];
int i = 0;
int lc = 0;
int uc = 0;
int numbers = 0;
int special = 0;

cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;

cin >> s;
cout << endl;
while (s[i] !=0)
{
if (s[i] >= 'a' && s[i] <= 'z')
{lc++;}
else
{if (s[i] >= 'A' && s[i] <= 'Z')
    {uc++;}
    else {if (s[i] >= '0' && s[i] <= '9')
            {numbers++;}

}

}
i++;
}
cout << "letters: " << lc + uc << " \nnumbers: " << numbers << endl;

return 0;
}
Last edited on
Quit spitting out code, TheSmallGuy, and give some explanation.

I'm not sure what characters would count as 'special' characters, but you can consult the ASCII table http://www.asciitable.com/ to see what the ASCII value is for a specific character. If you want to compare with it, you can cast the char to an int and compare it with the value in the ASCII table

Or if you just want any character that isn't '0' - '9', 'a' - 'z', or 'A' - 'Z' to be special then TheSmallGuy's code would work.
instead of :

else {if (condition) {} }

Do this:

1
2
3
4
5
6
else if (condition) {
    code
}
else {

}


It's possible this is yet another Troll Topic - hopefully not.

> Quit spitting out code, TheSmallGuy, and give some explanation.

Just quit altogether TheSmallTrollGuy, quit stealing our oxygen.
Last edited on
closed account (LNboLyTq)
You seem to forget to print out the total number of special characters you have counted too.
1
2
cout << "letters: " << lc + uc << " \nnumbers: " << numbers << endl;
cout << "specials : " << special << endl;
Last edited on
special characters are characters that that are not a letter or a number such as ! or $. Their Ascii numbers fall between the numbers for capital and lowercase letters in some instances.
Here is my code revised and explained.

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
#include <iostream>
using namespace std;
int main()
{
char s[50];
int i = 0; // used to help the program know when to end
int lc = 0; // holds the number of lowercase letters
int uc = 0; // holds the number of uppercase letters
int numbers = 0; // holds the amount of numbers 
int special = 0; //  holds the number of special characters.

cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;

cin >> s; // obtains the string
cout << endl;
while (s[i] !=0) // creates a conditional loop
{
if (s[i] >= 'a' && s[i] <= 'z')
{lc++;} // counts the number of lowercase letters
else
{if (s[i] >= 'A' && s[i] <= 'Z')
    {uc++;} // counts uppercase letters after all lowercase letters have been counted
    else {if (s[i] >= '0' && s[i] <= '9') // counts numbers after uppercase letters.
            {numbers++;}

// I am not certain how to go about counting special characters therefore it is left blank
}

}
i++;
}
cout << "There are " << lc+uc << " letters, " << numbers << " number(s), and " << special << " special characters" << endl;
// displays the total number of letters and numbers and eventually special characters once i figure out how to do so.


return 0;
}
Last edited on
I'm assume there is a simpler way of doing this rather than making 5 million if/else statements. If that is the only way then I guess i will give it a shot.
I got the program to work but it seems like a very long way of doing it. If anyone has any short-cuts please share. Thanks.

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
// PROG07.CPP       Mark Johnson        Counts characters in a string.
#include <iostream>
using namespace std;
int main()
{
char s[50]; // Creates string.

int i = 0; // Creates a variable to allow the while loop to know when to stop.

int lc = 0; // Each variable allows for the program to
int uc = 0; //  insert a total value for each type of character
int numbers = 0; // after the while loop has finished.

int special1 = 0;// 4 of theses are required due to the ASCII
int special2 = 0;// numerical system having values for special
int special3 = 0; // characters mixed along side values for
int special4 = 0; // letters and numbers.

cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;
// ask user for string and stores in "s"
cin >> s;
cout << endl;
while (s[i] !=0) // while loop that runs until 0 is reached
{
if (s[i] >= 'a' && s[i] <= 'z')//calculates lowercase characters
{lc++;}
else
{if (s[i] >= 'A' && s[i] <= 'Z')//calculates uppercase characters
    {uc++;}
else {if (s[i] >= '0' && s[i] <= '9')//calculates numbers characters
    {numbers++;}
else {if (s[i] >= 32 && s[i] <= 47) //calculates first set of values containing special characters.
    {special1++;}
else {if (s[i] >= 58 && s[i] <= 64) // calculates second
    {special2++;}
else {if (s[i] >= 91 && s[i] <= 96) // calculates third
    {special3++;}
else {if (s[i] >= 123 && s[i] <= 126) // calculates fourth
    {special4++;}

}
}
}
}
}
}
i++; // allows while loop to end
}
int sp = special1 + special2 + special3 + special4; // adds special character values
int total = lc + uc + numbers + sp; // adds total values
cout << "There are " << total << " total characters in the string." << endl;
cout << "There are " << lc+uc << " letter(s), " << numbers << " number(s), and " << sp << " special character(s)." << endl;

return 0;
}
Last edited on
Come on, man. Use code tags. Does that look readable to you?

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
72
73
74
75
// PROG07.CPP Mark Johnson Counts characters in a string.
#include <iostream>
using namespace std;
int main()
{
  char s[50]; // Creates string.

  int i = 0; // Creates a variable to allow the while loop to know when to stop.

  int lc = 0; // Each variable allows for the program to
  int uc = 0; // insert a total value for each type of character
  int numbers = 0; // after the while loop has finished.

  int special1 = 0;// 4 of theses are required due to the ASCII
  int special2 = 0;// numerical system having values for special
  int special3 = 0; // characters mixed along side values for
  int special4 = 0; // letters and numbers.

  cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;
  // ask user for string and stores in "s"
  cin >> s;
  cout << endl;
  while (s[i] != 0) // while loop that runs until 0 is reached
  {
    if (s[i] >= 'a' && s[i] <= 'z')//calculates lowercase characters
    {
      lc++;
    }
    else
    {
      if (s[i] >= 'A' && s[i] <= 'Z')//calculates uppercase characters
      {
        uc++;
      }
      else {
        if (s[i] >= '0' && s[i] <= '9')//calculates numbers characters
        {
          numbers++;
        }
        else {
          if (s[i] >= 32 && s[i] <= 47) //calculates first set of values containing special characters.
          {
            special1++;
          }
          else {
            if (s[i] >= 58 && s[i] <= 64) // calculates second
            {
              special2++;
            }
            else {
              if (s[i] >= 91 && s[i] <= 96) // calculates third
              {
                special3++;
              }
              else {
                if (s[i] >= 123 && s[i] <= 126) // calculates fourth
                {
                  special4++;
                }

              }
            }
          }
        }
      }
    }
    i++; // allows while loop to end
  }
  int sp = special1 + special2 + special3 + special4; // adds special character values
  int total = lc + uc + numbers + sp; // adds total values
  cout << "There are " << total << " total characters in the string." << endl;
  cout << "There are " << lc + uc << " letter(s), " << numbers << " number(s), and " << sp << " special character(s)." << endl;

  return 0;
}
@markjohnson6842

Not using code tags and not taking notice of advice are good ways of not getting help.

http://www.cplusplus.com/doc/tutorial/control/
my apologies, i am new to this cite and I am new to C++. I'm am not quite sure how to use code tags but i will learn. I also did not want to come across as rude I just felt that I needed to further explain my situation. Also, some of the formattings of the program is due to the format that I am required to keep it in by my instructor and may be because i simply don't understand
Topic archived. No new replies allowed.