Finding the occurrences of each letter in a string

Hello. This is my problem:
"Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter."


For example, the input line
I say Hi.
should produce output similar to the following:
3 words
1 a
1 h
2 i
1 s
1 y


I did only the "3 words" part:


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<string>
#include<iostream>
#include<cctype>
#include<algorithm>
using namespace std;
int main()
{
	int count=0;
	string input;
	getline(cin, input);
	for (int i = 0; i < input.length(); i++)
	{
		if (isspace(input[i]))
		{
			count++;
		}
	}
	count += 1;
	cout << "Number of words: " << count << endl;

	


	system("Pause");
	return 0;
}





I'd be grateful for any hints.
Last edited on
Counting would be a better title for your post.
I posted something similar a while back and managed to find it here:
http://www.cplusplus.com/forum/general/108911/

You have a few options you can use a std::map<char, int> which would be similar to that first digit integer count code I linked too.
You could also have a std::vector<int> occurrences(26, 0); //size 26 array initialized with 0s . You could access the array by doing occurrences[ tolower(input[i]) - 'a' ]. You already have ccytpe included so using tolower is okay.
Topic archived. No new replies allowed.