How will i make a program that will read a line of text and output the number of occurance each letter. Assume that the input characters must consist entirely of letters, whitespaces, commas, and period.
How will i make a program that will read a line of text and output the number of occurance each letter. Assume that the input characters must consist entirely of letters, whitespaces, commas, and period.
>>How will i make a program...
... do some research!
Find out how to getline from a user.
Find out how to loop.
Find out how to compare two letters.
Write something, and post it here when you're stuck.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>How will i make a program that will read a line of text and output the number of occurance each letter.
Of course, if this is a homework problem that you were hoping we would write for you, the above code is useless. But it may give you some ideas.Code:#include <iostream> #include <map> int main() { std::map<const char, int> frequency; std::map<const char, int>::iterator it; char input; while ( std::cin.get ( input ) ) { it = frequency.find(input); if ( it == frequency.end() ) frequency[input] = 1; else it->second++; } for ( it = frequency.begin(); it != frequency.end(); ++it ) std::cout<< it->first <<": "<< it->second <<std::endl; return 0; }
Toodles!
-Prelude
My best code is written with the delete key.
can u give me a simplier code or program. my professor kinda new. he don't like complicated stuff which will scratch his head for week reading that code. thanks for the reply goddess Prelude
and i'm kinda stuck
>>can u give me a simplier code or program.
No. Start working it out yourself, then post some code when you're stuck.
Read this in full before posting again please.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>my professor kinda new.
That was the point entirely. We don't write your homework for you, that defeats the purpose of it.
>he don't like complicated stuff which will scratch his head for week reading that code.
This sounds like you need a new professor. If your instructor can't figure out a simple frequency check with (very) simple STL usage, she/he shouldn't be teaching C++.
Here is a hint for a simple implementation that may work for you. Build two arrays, one that holds all of the letters being input and one that holds the frequency of each letter. The two array indices should correspond:
char letters[256];
int frequency[256];
if letters[5] == 'a' then
frequency[5] == number of a's
When you read a letter, search the letters array from start to finish. If the letter already exists then add one to the number in the corresponding frequency index. If the letter does not exist then add it to the end of the array.
This is a messy and inelegant solution and I probably completely confused you, but it is still a solution.
-Prelude
My best code is written with the delete key.
thanks prelude. and i think i need a new professor.
i'll work it out from your hint it's a great help. till again
I would suspect very few c++ profesors would be able to
say for certain if the code is absoutly correct since most professors arn't up to date with the latest
libraries Try to understand this sort
http://www.cs.cf.ac.uk/user/C.L.Mumf...CountPage.html
Dear Prelude here's wat i have came up
Try this on for size: My C++ teacher doesn't even know what a class is!
(but this is just a highschool and he's the only one who is willing to teach it. And he learned it specifically for the class, I believe)
You are right not all teachers know C++ and all the benefits of using the STL.
Beside they have a lot of non-certified teachers teaching programming in high school (C++ & Java). Those that do (most) teach towards the AP CS exam.
Mr. C.
>Dear Prelude here's wat i have came up
Have you tried to compile this? If not (most likely) then you should, because there are plenty of syntax errors that will keep the program from working properly.
As for the logic, it needs work. Here is pseudocode:
-PreludeCode:begin create input = 0 // Single character create letters[256] create frequency[256]; repeat i = 0 frequency[i] = 0 i = i + 1; until i >= 256 print "Enter Your Text Followed By EOF: " create letter_count = 0; while letter_count < 256 read input if input == EOF break; create exists = false; if input is letter // Process letters only repeat i = 0 // If input has already been read, increment the freqency if letters[i] == input frequency[i] = frequency[i] + 1; exists = true; break; endif until i >= letter_count // If input was not already processed, add // it and set the frequency to one if exists != true letters[letter_count] = input; frequency[letter_count] = 1; letter_count = letter_count + 1; endif endif loop repeat walk = 0 print letters[walk] + ": " + frequency[walk] + "\n"; until walk >= letter_count end
My best code is written with the delete key.