I have an array codes[26] which contains numbers inside {25,4646,23,12,.....29}

Each position in the array corresponds to a letter of the alphabet. code[0] is a, code[1] is b, etc...

I'd like to print the occurrences of each letter, something like this.

If there are 12 A's, 5 B's

.... so on for the rest of the letters.

Which approach can I take to accomplish this ?

You say that indexes of the array represents chars. Since indexes are unique there will be only one char for the corresponding index. How can there be 15 A where there is only one array[0] ?

And what those array elements are for? Where do you use them?

I didnt understand what your problem is. Please be more specific.

Sure, the array code[] does not store the letters, it stores the occurrences of the letters.

code = {23, 45, 2, 5, ...} up to 26 values in the array
Now I know that
code[0] = a (from above there are 25 A's)
code[1] = b (45 B's)
code[2] = c (2 C's)

From the problem you helped me solved earlier, that code populates the array code. It reads from a text file and counts the occurrences of letters and stores the final count onto code[]. Does that help?

Thanks again

Edited 6 Years Ago by MrDiaz: n/a


IMHO, I think what he means is that a[0] is 'A' and hence if the value of a[0] is 5(say) then the code must print AAAAA (i.e. 5 A's)

Correct me if I am wrong.

I was right :) ( i didn't see his explanation first, otherwise I wouldn't have posted the previous one)

Well as far as solution is concerned....

A small hint:

Use the value of your array in a loop condition and print A's or B's or whatever.

Edited 6 Years Ago by NP-complete: n/a

Yeah I'm thinking that but in my head looks like a really messy code.

Say I have to go through each member and just specify that code[0] = 'a' for every character? Seems rather long to me, I'm looking for an easier way to do this

Ok, I got it know :)

You did the big part of the program then :) You just need to design a loop that runs for every alphabet( 26 times ). Then in it design another loop that prints alphabets. Inner loop must print the current alphabet as much as it's occurence.

If you got stuck let us know.

Nope. You dont have to. you already know that right. You can fool your computer anytime. If I'm thinking it right then you need 2 loops. One will iterate through your code array. That is it must look like:

for (int i = 0; i<26; i++)

and the next comes nested under the first one and goes something like:

for (int j = 0; j<code[i]; j++)

in this 2nd loop you do the printing.

One more thing, you can use a char variable to print your alphabets

char myChar = 'A';

and then inside the 2nd loop you print this (as many times the loop iterates) and then you INCREMENT it so that it now becomes 'B';

myChar++    //will make it 'B' if it was 'A'

Hope it helps.

Edited 6 Years Ago by NP-complete: n/a

Great stuff! I had no clue this existed, very very handy! Thanks a bunch!

I have a quick question.

I'm getting aaaabbbbbcccc, how can I get a line break in that loop to separate the letters from each other?

Inside the first loop just prints

as expected. Inside the second loop gives me a lot of white space

great...in that case just mark this thread solved. And if any of my answers impressed you, add a reputation...:)

Edited 6 Years Ago by NP-complete: n/a

This article has been dead for over six months. Start a new discussion instead.