counting letter occurences with strings

I am trying to write a program to take in a string of text from the user, output the number of words in the text, then count the number of occurrences of each letter in the text that was typed in.

Here 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{

    int i, numspaces;
	char nextChar, answer;
	string msg;
	
    do
	{

	numspaces=1;

	cout << "Please enter a string of text for input: \n";
	getline(cin, msg);

	
	for (i=0; i<int(msg.length()); i++)
	{
		nextChar = msg.at(i);
		if (isspace(msg[i]))
			numspaces++;
	}

	cout << "\nThere are " << numspaces << " words in the input; press 'Enter' to view occurence counts.";
	cin.ignore();
	
	int count1 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'a' || msg[i] == 'A')
    ++count1;
    
    int count2 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'b' || msg[i] == 'B')
    ++count2;
    
    int count3 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'c' || msg[i] == 'C')
    ++count3;
    
    int count4 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'd' || msg[i] == 'D')
    ++count4;
    
    int count5 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'e' || msg[i] == 'E')
    ++count5;
    
    int count6 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'f' || msg[i] == 'F')
    ++count6;
    
    int count7 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'g' || msg[i] == 'G')
    ++count7;
    
    int count8 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'h' || msg[i] == 'H')
    ++count8;
    
    int count9 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'i' || msg[i] == 'I')
    ++count9;
    
    int count10 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'j' || msg[i] == 'J')
    ++count10;
    
    int count11 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'k' || msg[i] == 'K')
    ++count11;
    
    int count12 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'l' || msg[i] == 'L')
    ++count12;
    
    int count13 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'm' || msg[i] == 'M')
    ++count13;
    
    int count14 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'n' || msg[i] == 'N')
    ++count14;
    
    int count15 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'o' || msg[i] == 'O')
    ++count15;
    
    int count16 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'p' || msg[i] == 'P')
    ++count16;
    
    int count17 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'q' || msg[i] == 'Q')
    ++count17;
    
    int count18 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'r' || msg[i] == 'R')
    ++count18;
    
    int count19 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 's' || msg[i] == 'S')
    ++count19;
    
    int count20 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 't' || msg[i] == 'T')
    ++count20;
    
    int count21 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'u' || msg[i] == 'U')
    ++count21;
    
    int count22 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'v' || msg[i] == 'V')
    ++count22;
    
    int count23 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'w' || msg[i] == 'W')
    ++count23;
    
    int count24 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'x' || msg[i] == 'X')
    ++count24;
    
    int count25 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'y' || msg[i] == 'Y')
    ++count25;
    
    int count26 = 0;
    for (int i = 0; i < msg.size(); ++i)
    if (msg[i] == 'z' || msg[i] == 'Z')
    ++count26;
    
    
    cout << "A occurs: " << count1 << "times" << endl;
    cout << "B occurs: " << count2 << "times" << endl;
	cout << "C occurs: " << count3 << "times" << endl;
	cout << "D occurs: " << count4 << "times" << endl;
	cout << "E occurs: " << count5 << "times" << endl;
	cout << "F occurs: " << count6 << "times" << endl;
	cout << "G occurs: " << count7 << "times" << endl;
	cout << "H occurs: " << count8 << "times" << endl;
	cout << "I occurs: " << count9 << "times" << endl;
	cout << "J occurs: " << count10 << "times" << endl;
	cout << "K occurs: " << count11 << "times" << endl;
	cout << "L occurs: " << count12 << "times" << endl;
	cout << "M occurs: " << count13 << "times" << endl;
	cout << "N occurs: " << count14 << "times" << endl;
	cout << "O occurs: " << count15 << "times" << endl;
	cout << "P occurs: " << count16 << "times" << endl;
	cout << "Q occurs: " << count17 << "times" << endl;
	cout << "R occurs: " << count18 << "times" << endl;
	cout << "S occurs: " << count19 << "times" << endl;
	cout << "T occurs: " << count20 << "times" << endl;
	cout << "U occurs: " << count21 << "times" << endl;
	cout << "V occurs: " << count22 << "times" << endl;
	cout << "W occurs: " << count23 << "times" << endl;
	cout << "X occurs: " << count24 << "times" << endl;
	cout << "Y occurs: " << count25 << "times" << endl;
	cout << "Z occurs: " << count26 << "times" << endl;
	
	msg.erase(1, 20);
	cout << "Would you like to test again? \n";
	cin >> answer;
	
}	while (answer == 'Y' || answer == 'y');

	system ("pause");
    return 0;
}


This all executes and runs okay, but when I try and test the program again after being asked to, the text input phase is skipped and the number of words is automatically set to one. The program is then closed after pressing enter.

Also, I do recognize that the way I chose to write this code is probably unconventional. If anyone could suggest a better way to write this, maybe using less lines than I did?

Thanks
You should probably just have an array of 'count'. Whenever you have a bunch of variables with some name then a number, you pretty much are looking at an array. Then, when you loop through the message, only do it once; check each character and increment the appropriate count rather than doing one pass for each different character.

What do you mean by the text input phase is skipped? Could you show a quick example of your program being run in the console?
getline() extracts characters from the stream including the delimiter, which is '\n' by default. >> extracts until a delimiter (many things, but including '\n'). The second time your loop loops, there is a '\n' in the buffer, and this is enough to skip the entire count portion of your code.

So, when you type the next line, you are really extracting the first charcter and checking if it equals 'y' or 'Y'

try cin.ignore() after line 193.
Okay, thanks I'll do that. By that I mean that after entering 'y', instead of the program starting over from the beginning, the part

cout << "Please enter a string of text for input: \n";
getline(cin, msg);

seems to get skipped and the user isn't allowed to enter a line of text. The number of words is then just set to 1. I'm trying to start over from the beginning after pressing 'y' so that I can run the program again.
Last edited on
getline() extracts characters from the stream including the delimiter, which is '\n' by default. >> extracts until a delimiter (many things, but including '\n'). The second time your loop loops, there is a '\n' in the buffer, and this is enough to skip the entire count portion of your code.

So, when you type the next line, you are really extracting the first charcter and checking if it equals 'y' or 'Y'

try cin.ignore() after line 193.


Thanks the cin.ignore() made it work right.

You should probably just have an array of 'count'. Whenever you have a bunch of variables with some name then a number, you pretty much are looking at an array. Then, when you loop through the message, only do it once; check each character and increment the appropriate count rather than doing one pass for each different character.


So I will just use another for loop for count right? Will this make the user have to input the characters?

You don't have to. You can just check for 'a's and put that into slot 0, 'b's into slot 1, and so on for an array of size 26. You can note that chr - 'a' == ind where 'chr' is the character you are looking for and 'ind' is the index into the array where you want to store it. You may want to convert the entire string to lowercase as well so you only have to look for one case.
Topic archived. No new replies allowed.