please help me with this problem... i dont have any idea on how to make the code...

Histogram is a process of representing the frequency of data in a
chart. Write a C++ program that will simulate this process. Your
program should read a string (only letters are allowed) and output
its distinct letters in alphabetical order. Adjacent to the letters
are asterisks (*) that represent the number of times the letter
appeared in the given string.

Sample Input/Output
Enter a string: joshua
Histogram for joshua

a *
h *
j *
o *
s *
u *
another? [y/n] -> y
Enter a string: waka waka fifa
Histogram for waka waka fifa
a *****
f **
i *
k *
w **

First you can sort the string using sort, defined in <algorithm>:

string s = "car";

	sort(s.begin(), s.end());

Then you print it to the screen:

for(int i = 0; i != s.size(); i++) {
		cout << s[i];

		//Here you can insert a for loop to count how many
		//times s[i] is repeated in the string.
		cout << endl;

And after you made that loop you must find a way to make it print the letters only once.

what would be the syntax for the loop on ho many times s appears?...

i try to use the code but the character is still repeated... like, i change the string to carr, r s still repeated in the sorted letters, which should not be... i cant really figured out the code..

Often with counting/histogram problems, you can make an array of counters, one for every possible value you are counting.

#include <iostream>
using namespace std;
int main()
  unsigned char uc = 'a';  // the unsigned version is just a non-negative number
  cout<< "the numerical value for \'" << uc << "\' is: "<< (int)uc << endl;
  uc= -1;
  cout<< "the maximum number a character can hold is: "<< (int)uc<< endl;
  return 0;

.. And remember, any non-negative number can be used as an array index. Already I have said too much.

this is my code and its already running... Now, my problem is, how will i make this to show the histogram chart in vertical orientation like this?

Sample Input/Output
Enter a string: waka waka fifa
Histogram for waka waka fifa
* * *
* * * * *
a f i k w

# include <iostream>
# include <string>
using namespace std;
  string letters="abcdefghijklmnopqrstuvwxyz";
  string sentence;
  string::size_type slen;
  int name[26];
  char ans;

  for(int s=0; s < 26; s++)
    name[s] = 0;
  cout<<"Enter a string (no space please): ";
  cout<<"Histogram for "<<sentence<<endl;
  slen = sentence.length();
  for(int t=0; t<26; t++){
    for(int sl=0; sl<slen; sl++){
      if (letters[t] == sentence[sl])
  for (int r=0; r < 26; r++){
    if (name[r] > 0){
      cout<<letters[r]<<" ";
    for(int i=0; i <name[r]; i++)
      cout << endl << "another? [y/n]: ";
      cin >> ans; 
}while(ans != 'n'); 

      cout<<"\n\nPress any key and ENTER to EXIT! ";         
      char response;
      return 0;

Nice job on the solution! It works like a charm, as long as your input is lower case. You could use the tolower() function on input to normalize for case.

The least you will need in order to rotate your output to vertical orientation, is to find the maximum count for any character. Once you have that.. (you know the rest).

For ASCII characters, there is a shortcut you can use for counters, which takes advantage of the fact that characters are small numbers:

# include <string>
using namespace std;
int* histogram(string s,int &last_found,int &max)
	static int counters[1 << 8* sizeof(char)]; // counter for each possible byte
	last_found= 0;
	for( string::iterator i=s.begin(); i!=s.end(); ++i )
// here is the critical trick:
		unsigned char c= (unsigned char)tolower(*i); // 'unsigned char' is one byte
// .. and one byte is a number between zero and 255 ; perfect for an array index
		if(++counters[c] > max) max = counters[c]; // use the byte as an index
		if( c > last_found ) last_found = c;
	return counters;

.. and you can call the function like this:

getline(cin,sentence); // this allows spaces

		int last,max,*name;
		name= histogram(sentence,last,max);

		for (unsigned char r=0; r <= last; r++){
			if (name[r] > 0){
				cout<<r<<" ";
				for(int i=0; i <name[r]; i++)
		cout << "Max found for any char: "<< max
			<<".\n\nAnother? [y/n]: ";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.