#1 C++ programming

DBELL1981
DBELL1981 used Ask the Experts™
on
How would u write a C++ program that would read in a line of text and output the number of occurrences fo each letter in the line?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
It's against EE policy to do homework questions.

Author

Commented:
< removed by modulo CS moderator EE >

Commented:
LOL Axter, look what you have done again! :)
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Commented:
You seem like a smart guy DBELL1981, from all the big words you use.  
But if your so smart can't you write a little code by yourself?

(Insight does not mean write the code for you)
Commented:
Create array for every possible letter.
Run over text and add 1 whenever a letter occurs.

Commented:
Create a map of <char, int> which will contain a letter as key and the number of occurences as value.
Then each time a letter is read look for the letter in the map (the char is the key in the map).
If there is already such letter increase the counter (the value in the map)
If there is none than add the letter to the map as key with the value of 1.
This option is more complicated than the one suggested by Hoegie. In my opinion it is better but I do not want to bring up a discussion of code clearness and simplicity against efficiency and memory usage.
For the small number of letters of the alphabet (maybe you want to join counts of capital letter appearences with lower case ones and maybe you dont) the usage of a fixed array as Hoegie suggest is quite OK ... and very simple.
For other cases in which the array might be very large - e.g. if the character set is Chinese or Japanese - I think that the map option is much better. In these cases you will need also to use a wchar (16-bit Unicode character) instead of a char.

Commented:
easier than that.
Each ASCII character (A-Z, a-z) is in sequential order from I think 42 up.  So array[0] would be A, array[1] would be b.  

First clear the array - set all indices to 0.
Next read in the line and parse each letter incrementing

array[char - 'A']++.

Next go through the array and print the results.

...and by the way, refrain from the strong language.  It might get you kicked outta here!

JB
DBELL1981,
This looks like a homework question to me.
Why are you trying to do this project.

I suggest you try reading your text book, and then trying doing the work yourself, instead of getting other people to do it for you.

In any case your comments are not proper ettiquete for Experts-Exchange.

If you want to carry on in that manner, try going to the newsgroup where you're allowed to say anything you want.

EE requires a little more civilized behavior.

Commented:
DBELL1981,

As stated above by many experts, we like to behave in a mature way on this site.
The assumption that this is a homework question is not abusing, your comment however is.
That's why I've removed your comment.

You can try to explain why this isn't (as I think too) a homework question. When you're looking however for solutions or completely written code, you're here in the wrong place. Experts help people that got stuck, but did take the effort to solve the problem theirselves first !

When I receive no comment from you in 72 hrs I'll accept the comment from Hoegje as an answer as that's the first that gives a general solution direction.

modulo

Community Support Moderator
Experts Exchange

Commented:
In the deleted statement DBELL1981 says it isn't homework but perhaps a question in a beginning C++ book - I will back him/her on that.  A lot of people program out of curiosity, or looking for a better way to make a living.  Programming, no matter what anyone says, it still a lucrative career.

However, he/she could have just stated that and moved on.  He/she is a newbie and probably used to newsgroups where flaming is commonplace.

JB

Commented:
jimbucci,
Even if this is from a beginner's C++ book, you're still not going to learn very much if you ask some one to do it for you.
He should attempt to do the work, and then ask questions when he gets stuck.

Not ask some one to do it for him.

Commented:
I disagree.
You can give someone an idea of how to do something without actually giving them code.  
Also, looking at other people's code does actually help a person learning the language - to see how someone else solved the problem.  If the person blindly uses the code without figuring how it really works then, yes, you learn nothing.

JB

Commented:
Hi jimbucci,

The only way to learn is to do.
As an acient philosopher said:
I hear I forget
I see I remember
I do I know

modulo

Community Support Moderator
Experts Exchange

Commented:
Not entirely true.  If it can be proved that one person learned by example, then such a saying is wrong.  You cannot group all people into the same category - people are not made that way.

JB
EOL

Commented:
jimbucci
Man, haven't you read some of the fine philosophers words?

Also here's the solution, if you understand it you will know how it produces the right answer ( altough unsorted )

map<char, int> occ;
     map<char, int>::iterator occIter;
     string s("This is a test string, hope you understand it");

     for( string::iterator i = s.begin(); i != s.end(); i++ )
     {
          if( ( occIter = occ.find(*i) ) == occ.end() )
               occ[ *i ] = 1;
          else
               occ[ *i ]++;
     }

     for( occIter = occ.begin(); occIter != occ.end(); occIter++ )
          cout << "\"" << occIter->first << "\" = " << occIter->second << endl;

What will be the next tasks for you is to figure out what headers you need for this to work. If you know look up what the objects do. If you'v understand the code you'll have gained another height of c++ expirience. Ah, and here the output for the test sentence.

" " = 8
"," = 1
"T" = 1
"a" = 2
"d" = 2
"e" = 3
"g" = 1
"h" = 2
"i" = 4
"n" = 3
"o" = 2
"p" = 1
"r" = 2
"s" = 5
"t" = 5
"u" = 2
"y" = 1

Commented:
EOL just implemented my suggested solution.
Usually we expect the person asking the question to try and implement it imself using EE guidlines and then we help fixing and improving the code presented.
In this case you were given 99% of the solution, just add the correct headers as EOL says and needed using declarations.
You were given a lot of material to study as the code here uses stl containers and iterators

Commented:
As there were no objections to my proposal:

force accepted as proposed

modulo

Community Support Moderator
Experts Exchange

Author

Commented:
First off i would like to apologize to the site for my profound actions.I went of and i would like to apologize.But first of all its like this this is a project that i am doing for myself i have no help with this what so ever i am a beginner at C++ and i came to this site for some ideas and insight.i am working on this solo project so i can get a better understanding in C++ so when the time comes i will be ready to do what i go to do......

Commented:
Hi DBELL1981,

No need to apologize, but it sure show's character to do so !

We're here helping people as much as we can, but we also try to prevent "cheating" of students as it wouldn't be fair to the student, the fellow students and the teacher when we provide here copy/paste solutions.

The goal of every training program will be the "getting to know the trade". And just by seeing (as stated above) a solution on forehand doesn't work as good as having tried it yourself first to see the true value of working code.
Besides that I always feel a kind of joy having solved a programming puzzle and that feeling isn't there when you have a pre-coocked solution.

For the real general directions of a language you could ask here for some good sites with background information about C++, but in my experience it's hard to grasp the idea behind a language without having coded some lines. Never the less a good story about the "how and why" f a language might also speed up getting the language to know.

Success with your project and I hope our experts will be able to help you when stuck.

modulo

Community Support Moderator
Experts Exchange

BTW when you have gathered some knowledge, just try to answer some Q's here. It's often surprising how much you learn only from trying to help oyhers !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial