Count characters in a string

Hi im new to coding and as an excercise i tried to make a program that gets a sentence or string from the user, then displays the number of a's, b's, c's, etc... in the string. i have 2 questions:

1. my code isn't working. it will only count up to the first space. why is this?
2. this seems like a lot of code for a simple task. is there an easier way to do it?
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

string userString = "";

int main()
{
	cout << "Enter a phrase:";
	cin >> userString;

	int a = count(userString.begin(),userString.end(),'a');
	int b = count(userString.begin(),userString.end(),'b');
	int c = count(userString.begin(),userString.end(),'c');
	int d = count(userString.begin(),userString.end(),'d');
	int e = count(userString.begin(),userString.end(),'e');
	int f = count(userString.begin(),userString.end(),'f');
	int g = count(userString.begin(),userString.end(),'g');
	int h = count(userString.begin(),userString.end(),'h');
	int i = count(userString.begin(),userString.end(),'i');
	int j = count(userString.begin(),userString.end(),'j');
	int k = count(userString.begin(),userString.end(),'k');
	int l = count(userString.begin(),userString.end(),'l');
	int m = count(userString.begin(),userString.end(),'m');
	int n = count(userString.begin(),userString.end(),'n');
	int o = count(userString.begin(),userString.end(),'o');
	int p = count(userString.begin(),userString.end(),'p');
	int q = count(userString.begin(),userString.end(),'q');
	int r = count(userString.begin(),userString.end(),'r');
	int s = count(userString.begin(),userString.end(),'s');
	int t = count(userString.begin(),userString.end(),'t');
	int u = count(userString.begin(),userString.end(),'u');
	int v = count(userString.begin(),userString.end(),'v');
	int w = count(userString.begin(),userString.end(),'w');
	int x = count(userString.begin(),userString.end(),'x');
	int y = count(userString.begin(),userString.end(),'y');
	int z = count(userString.begin(),userString.end(),'z');

	if (a > 0)
	{
		cout << "string contains " << a << " a's" << endl;
	}
	if (b > 0)
	{
		cout << "string contains " << b << " b's" << endl;
	}
	if (c > 0)
	{
		cout << "string contains " << c << " c's" << endl;
	}
	if (d > 0)
	{
		cout << "string contains " << d << " d's" << endl;
	}
	if (e > 0)
	{
		cout << "string contains " << e << " e's" << endl;
	}
	if (f > 0)
	{
		cout << "string contains " << f << " f's" << endl;
	}
	if (g > 0)
	{
		cout << "string contains " << g << " g's" << endl;
	}
	if (h > 0)
	{
		cout << "string contains " << h << " h's" << endl;
	}
	if (i > 0)
	{
		cout << "string contains " << i << " i's" << endl;
	}
	if (j > 0)
	{
		cout << "string contains " << j << " j's" << endl;
	}
	if (k > 0)
	{
		cout << "string contains " << k << " k's" << endl;
	}
	if (l > 0)
	{
		cout << "string contains " << l << " l's" << endl;
	}
	if (m > 0)
	{
		cout << "string contains " << m << " m's" << endl;
	}
	if (n > 0)
	{
		cout << "string contains " << n << " n's" << endl;
	}
	if (o > 0)
	{
		cout << "string contains " << o << " o's" << endl;
	}
	if (p > 0)
	{
		cout << "string contains " << p << " p's" << endl;
	}
	if (q > 0)
	{
		cout << "string contains " << q << " q's" << endl;
	}
	if (r > 0)
	{
		cout << "string contains " << r << " r's" << endl;
	}
	if (s > 0)
	{
		cout << "string contains " << s << " s's" << endl;
	}
	if (t > 0)
	{
		cout << "string contains " << t << " t's" << endl;
	}
	if (u > 0)
	{
		cout << "string contains " << u << " u's" << endl;
	}
	if (v > 0)
	{
		cout << "string contains " << v << " v's" << endl;
	}
	if (w > 0)
	{
		cout << "string contains " << w << " w's" << endl;
	}
	if (x > 0)
	{
		cout << "string contains " << x << " x's" << endl;
	}
	if (y > 0)
	{
		cout << "string contains " << y << " y's" << endl;
	}
	if (z > 0)
	{
		cout << "string contains " << z << " z's" << endl;
	}
	system("pause");
}
Hi nathansmith72, welcome to cplusplus forums :)

first of all, your program only works for the first space because that's all that it's being given. cin's operator >> uses ' ' as a delimiting character, so it will pull only up to the first space. Instead, use getline(cin, userString);.
That will resolve your first issue.

Secondly, yes, the code can be vastly reduced.
I would approach this problem by playing with ASCII values, which is what chars are represented as. http://www.asciitable.com/

lower case 'a' has an ASCII value of 97. (chars are actually symbols assigned a numerical value). lower case 'z' has an ASCII value of 122, and the rest of the lowercase alphabet is in between. This is important because you can treat chars somewhat like integers. So what's really cool is that you can actually increment a char in a for loop.

1
2
3
4
5
6
for(char index = 'a'; index <= 'z', ++index)
{
    int count = count(userString.begin(), userString.end(), index);
    if(count > 0)
        cout << "string contains " << count << " " << index << "'s" << endl;
}
Last edited on
thanks! that helped a lot. the program ran great when i used getline. i then tried to shorten the code using the code u gave me but i ran into a problem i havent been able to resolve yet.

when the program goes to display the numbers it is now displaying the ascii value's instead of the count. I cant figure out where i messed it up.

edit: just wanna let u know i did try to fix it before i posted again lol. i cant figure out how count is getting set = to the ascii value

new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

string userString = "";

int main()
{
	cout << "Enter a phrase:";
	getline(cin, userString);

	for(char index = 'a'; index <= 'z'; ++index)
	{
		int count = (userString.begin(), userString.end(), index);
		if(count > 0)
        cout << "string contains " << count << " " << index << "'s" << endl;
	}
	system("pause");
}


thanks for you'r help =)
Last edited on
index has to be cast as an int:
cout << "string contains " << count << " " << (int)index << "'s" << endl;

index can be an int in the first place without any problems:
for(int index = 'a'; index <= 'z'; ++index)

That way you don't need the cast.
Last edited on
Can someone please help me with my program!? I've been trying to compile it but it will not run. Error shows up with case 1 and case 4. C++ coding. Something about classifying the strings. Please help me! Thank you

MAIN

#include <iostream>
#include <climits>
#include <string>
#include "bookpile.h"

using namespace std;

void myChoice();

int main()
{
myChoice();
int myChoice;
bookPile b1;
do
{
cin >> myChoice;
switch(myChoice)
{
case 1:
cout << "b1.addBook: " << b1.addBook() << endl; // increment a string
break;

case 2:
cout << "b1.removeBook: " << b1.removeBook() << endl;
break;

case 3:
cout << "b1.getTopBook: " << b1.getTopBook() << endl;
break;

case 4:
cout << "b1.getNthBook: " << b1.getNthBook() << endl;
break;

case 5:
cout << "b1.getAll: " << b1.getAll() << endl;
break;

case 6:
cout << "b1.getCount: " << b1.getCount() << endl;
break;
case 7:
break;
default:
cout << "Invalid input: " << endl;
}

} while (myChoice != 8);


system("pause");
return 0;
}

void myChoice()
{
cout << "MENU" << endl;
cout << "1: Add Book to top of the pile " << endl;
cout << "2: Remove Book from top of the pile " << endl;
cout << "3: Get Book on top " << endl;
cout << "4: Get Nth Books " << endl;
cout << "5: Get all of the titles " << endl;
cout << "6: Get the number of books on pile " << endl;
cout << "7: Exit " << endl;
cout << "Enter your choice : ";
}

HEADER

#ifndef BOOKPILE_H
#define BOOKPILE_H

//#include <iostream>
//#include <climits>
#include <string>

using namespace std;

class bookPile // book pile declaration
{
private:
//static const int books = 10; // same as 'n' books
int bookCount;
string n[10]; // 10 books, just the titles

public:

bookPile();
// string bookTitle() const;
string add() const;
bool addBook(string & item);
bool removeBook();
string getTopBook() const;
string getNthBook(int a) const;
//void getCount();
int getAll() const;
int getCount() const;

};


#endif

IMPLEMENTATION

//Implementation File
#include "bookpile.h"
#include <string>

using namespace std;


bookPile::bookPile()
{
bookCount = 0;
}


bool bookPile::addBook (string & item)
{
if (bookCount >= 10)
return false;
n[bookCount] = item;
bookCount ++;
return true;

}

bool bookPile::removeBook()
{
bookCount --;
}

string bookPile::getTopBook() const
{
return n[bookCount - 1];
}

string bookPile::getNthBook(int a) const
{
return n[a];
}

int bookPile::getAll() const
{
return bookCount;
}

int bookPile::getCount() const
{
return bookCount;
}
I found the problem with my program, i will highlight the error and the fix for anyone who might come by this post. sorry i cant help u prith =(

This:
1
2
3
4
5
6
7
8
	for(char index = 'a'; index <= 'z'; ++index)
	{
		int count = (userString.begin(), userString.end(), index);
		if(numb > 0)
        cout << "string contains " << count << " " << index << "'s" << endl;
	}
	system("pause");
}


should be this: (underlined the correction)
1
2
3
4
5
6
7
8
	for(char index = 'a'; index <= 'z'; ++index)
	{
		int count = count(userString.begin(), userString.end(), index);
		if(numb > 0)
        cout << "string contains " << count << " " << index << "'s" << endl;
	}
	system("pause");
}
@lowestOne
Index needs to be a char. It's intended to be output as such.

@nathansmith27
Your underlined correction was in my initial post! :p
Glad you got it working.
Last edited on
Topic archived. No new replies allowed.