Count occurrences of every character in a string

(Count occurrence of each letter in a string) Write a function that counts the occurrence of each letter in the string using the following header:
void count(const char s[], int counts[])
where counts is an array of 26 integers. counts[0], counts[1]…. and counts[25] count the occurrence of a,b,c, and z respectively. Letter are not case -sensitive. Ie. letter A and a are counted the same as a.
Write a test program that reads a string, invokes the count function and displays the non-zero counts.

Sample run of the program:
Enter a string: Welcome to New York!
c: 1 times
e: 3 times
k: 1 times
l: 1 times
m: 1 times
n: 1 times
o: 3 times
r: 1 times
t: 1 times
w: 2 times
y: 1 times

Here's the code I have so far but it stops once it gets to the 't' in 'to' and I'm not sure why and also I'm not sure how to make it not print letters that don't appear in the string. Any help would be appreciated. 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
  #include <iostream>
#include <string>
using namespace std;

const int size = 100;
const int numberOfLetters = 26;
void count(char [], int []);

int main()
{
	int counts[numberOfLetters];
	char s[size];


	cout << "Enter a string: ";
	cin.getline(s, '\n');

	

	count(s, counts);

	for (int i = 0; i < numberOfLetters; i++)
	{
		cout << counts[i] << " " << static_cast<char>(i + 'a') << endl;
	}

}

void count(char s[], int counts[])
{
	for (int i = 0; i < numberOfLetters; i++)
	{
		counts[i] = 0;
	}

	for (int i = 0; i < size; i++)
	{
		{
			s[i] = tolower(s[i]);
			counts[s[i] - 'a'] ++;
		}
	}	
}
Last edited on
I don't think you should loop all 100 elements in the array. The length of the string is probably much less.
How would I make it loop just the amount that's in the string? Should I make a function or something to count how many letters in the string first?
You can do that, or you could simply use std::strlen. http://www.cplusplus.com/reference/cstring/strlen/
And this is why you shouldn't be using global variables. Is the size of every string your program gets 100? Probably not

Also the second parameter for istream::getline() is the number of characters to read. In your program you are using '\n' which is equal to 10.

http://www.cplusplus.com/reference/istream/istream/getline/
Okay thank you! Now it does work so it goes through the string and counts all the letters, but it still prints the count for every letter including ones that didn't occur as '0'.

Could I do something like this to only display letters that occur? I have this as the last for loop in my main function.

1
2
3
4
5
6
7
8
9
10
11
12
13

for (int i = 0; i < numberOfLetters; i++)
{
     if (counts[i] = 0)
     {
          continue;
     }
     else
     {
          cout << counts[i] << " " << static_cast<char>(i + 'a') << endl;
     }
}
okay well that didn't work I'm not sure what to do to make it not print the ones that don't occur
In your latest code you are using = instead of ==.
You also have to initialize the count array.

1
2
// Initialize all elements to zero.
int counts[numberOfLetters] = {};

1
2
3
4
if(counts[i]>0)
{
	cout << static_cast<char>(i + 'a') << ": " << counts[i] << " times." << endl;
}


I managed to figure it out I just made it check for ones that occur rather than ones that don't and it worked. Thank you for all of your help!
Topic archived. No new replies allowed.