Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

How to calculate the frequency of each letter in a string with C

1 Answer

0 votes
#include <iostream>
#include <map>

int main()
{
	std::string s = "c c++ c# java python";
	std::map<char, std::size_t> frequency;

	for (auto ch : s)
	{
		if (frequency.find(ch) == frequency.end())
			frequency[ch] = 1;
		else
			frequency[ch]++;
	}

	for (auto element : frequency)
		std::cout << element.first << " : " << element.second << std::endl;

	return 0;
}



/*
run:

  : 4
# : 1
+ : 2
a : 2
c : 3
h : 1
j : 1
n : 1
o : 1
p : 1
t : 1
v : 1
y : 1

*/

 



answered Jun 13, 2017 by avibootz
...