Making string count numbers, blanks and alphabetical characters

Hello, I'm trying to create a program that counts the amount of alphabetical characters, numbers, blanks and total amount of characters in a string a user gets to enter. The user also quits the program manually by pressing CTRL + D (in linux terminal).

These are the results I get from the code so far by typing in "hello cplusplus 123 c++" CTRL + D.

1
2
3
4
5
The string had:
Alphabetical characters...: 0
Numbers...................: 0
Blanks....................: 3
Total amount of characters: 20


So I can't seem to find a way to count the amount of alphabetical characters and numeric characters. Can anyone help me out with this?

And here's 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
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <sstream>
using namespace std;

int main()
{
  string tecken;
  char blank = ' ';
  int teckenlangd = 0, nummerlangd = 0, total = 0, blanklangd = 1;

  while(cin >> tecken)
    {
      stringstream nummer(tecken);
      if(nummer >> nummerlangd)
	{
	  nummerlangd = nummerlangd + tecken.length();
	}
      else if(nummer >> tecken)
	{
	  teckenlangd = teckenlangd + tecken.length();
	}
      else if(cin.get() == ' ')
	{
	  blanklangd++;
	}

      total = total + tecken.length();
    }

  cout << "The string had:" << endl;
  cout << "Alphabetical characters " << teckenlangd << endl;
  cout << "Numbers" << setw(8) << setfill('.') << ": " << nummerlangd << endl;
  cout << "Blanks" << setw(9) << setfill('.') << ": " << blanklangd << endl;
  cout << "Total amount of characters" << setw(7) << setfill('.') << ": " << total << endl;

  return 0;
}
Here is my suggestion :

1
2
3
4
5
6
7
8
9
10
11
12
13
getline(cin, tecken);

for (int i = 0; i < tecken.length(); i++)
{
  if (tecken[i] >= '0' && tecken[i] <= '9')
    nummerlangd++;
  else if (tecken[i] >= 'a' && tecken[i] <= 'a')
    teckenlangd++;
  else if (tecken[i] <= 'A' && tecken[i] >= 'Z')
    teckenlangd++;
  else if (tecken[i] == ' ')
    blanklangd++;
}
@squished18 not guaranteed to work. There is little thing called codepages. It can ruin everything.

@OP open to yourself <cctype> header and isalpha(), isdigit() and isspace() functions.
http://en.cppreference.com/w/cpp/string/byte
@MiiNiPaa I'm aware of the isalpha(), isdigit() and isspace() functions and that is basically why I declared cctype in the first place but I have no clue of how I'm supposed to use them.
Last edited on
Oh, ok. You have problem with input: cin >> tecken will not read whitespaces. You have attempt to handle that inside your function, but there is several ways to fail it. For example your input has 23 characters and 4 spaces.

I would suggest to read whole line by using getline() function.

Then iterate over resulting line and check current characte against all three functions:
1
2
3
4
5
6
7
for(int i = 0; i < line.size(); ++i) {
    if( isspace(line[i]) )
        ++spaces;
    if( isdigit(line[i]) )
        ++digits;
//...
}
Thank you sir, that solved the problem.
Topic archived. No new replies allowed.