Reading a string and outputting the amount of times each letter occurs

closed account (jhb7Djzh)
I can't figure out how to efficiently store and print out every case of a certain letter in this program. I could just make a variable for every letter but I really want to find a better way to do this. Thanks.

The question states that an array of letters and numbers should be used, hence the numbers array.

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
 #include <iostream>
#include <string>

using namespace std;

int main(){
	
	char alp[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'y', 'z'};
	int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
	string input;
	getline(cin, input);
		
		int count = 0;
		
	for	(int i = 0; i < input.length(); i++)
	{
		switch(input.at(i))	   
		{	case 'a': count = numbers[i++]; break;
			case 'b': count = numbers[i++]; break;
			case 'c': count = numbers[i++]; break;
			//etc...
		}
	}
	
	cout << "Letter | # Of Occurances" << endl;
	
	for (int i = 0; i < 26; i++)
	{
		cout << alp[i] << " | " << count << endl;	 
	}	 
}
Last edited on
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
 #include <iostream>
#include <string>

using namespace std;

int main(){

    // fixed your char array, it was missing some letters
	char alp[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	int numbers[26] = {0};
	string input;
	getline(cin, input);


	for	(int i = 0; i < input.length(); i++)
	{
	    if(isalpha(input[i]))
            numbers[ input[i] % 26 ]++;
	}

	cout << "Letter | # Of Occurances" << endl;

	for (int i = 0; i < 26; i++)
	{

		cout << alp[i] << " | " <<  numbers[  alp[i] % 26 ] << endl;
	}
}




the quick brown fox jumped over the lazy dog
Letter | # Of Occurances
a | 1
b | 1
c | 1
d | 2
e | 4
f | 1
g | 1
h | 2
i | 1
j | 1
k | 1
l | 1
m | 1
n | 1
o | 4
p | 1
q | 1
r | 2
s | 0
t | 2
u | 2
v | 1
w | 1
x | 1
y | 1
z | 1

Process returned 0 (0x0)   execution time : 13.678 s
Press any key to continue.


Note that this code only works for lowercase letters. You'll need to modify it if you want it to count both upper and lower case, and modify further if you want it to count the upper and lower letters of the same type as a single letter (i.e. A and a are the same letter).
closed account (jhb7Djzh)
Thanks. Though I'm not sure I understand the following lines of code though:

 
numbers[ input[i] % 26 ]++;


and

 
numbers[  alp[i] % 26 ]
Too much work :) for ascii:

unsigned int tbl[256] = {0};

for(... j = each letter etc)
tbl[strvar[j]]++;

if you want to lump caps and lower case, toupper or tolower it before counting.


%26 is remainder of 26, which converts the value to 0-25, fitting it into an array.
but it will act oddly if you have punctuation, spaces, or such.

the above will count anything ... # of spaces, # of . ... # of A and number of a etc. easy to index also... want to see how many fs? its tbl['f'];

Last edited on
Topic archived. No new replies allowed.