129k views
3 votes
Write a C++ program to enter a text and count how many times one letter appear in the text?

User Sybohy
by
8.7k points

1 Answer

4 votes

Answer:

#include <iostream>

#include<map>//to inlude hashmap..

#include<string>

using namespace std;

int main() {

string sa;//declaring a string s.

map<char,int>m;// A hashmap of char type key and integer type value.

cout<<"Enter the text"<<endl;

cin>>sa;//taking input of the string..

for(int i=0;i<sa.length();i++)

{

m[sa[i]]++;//increasing the count of character in the hashmap..

}

for(auto i:m)//iterating oveer the hashmap m

{

cout<<i.first<<" "<<i.second<<endl;//displaying the character and the count..

}

return 0;

}

Step-by-step explanation:

I have taken a hashmap m and a string s .I am taking input of text in the string s.The hashmap is of type key =Char and value = int to store the count.After that iterating over the string and updating the count in the hashmap of each character and then printing the count of each character..

User Prashank
by
7.9k points