# Thread: counting letter occurences in a string

1. There is more than one way to solve the problem, but that method sounds good to me. One thing to note is that the middle part can be done more easily. Instead of counting characters based on the index, go through the input string and increment the count of each character in that string.

3. i'm having a hard time figuring out how to write the code to do that, also .. the code in my anagram i'm finding it difficult to see waht i can relate from the anagram code to this code

4. Originally Posted by pjr5043
i'm finding it difficult to see waht i can relate from the anagram code to this code
this is exactly how the code worked that both todd burch and i posted.

his is more directly applicable to this problem, whereas mine counted strictly alphabetical characters.

if you understood how those anagram programs worked, you should understand this one already.

5. alrite .. i'll have to look at them an analyze them more

6. alrite guys .. i reallly need some help with this, i tried looking at the previous code i had for my anagram program, but i cannot understand it at all.

here is an update:

the teacher wants me to use this function: "void count(const char myStr[])"
and she wants me to call this: "count("ABcaBadd*eekjdfefdeg,TTew44Tt")"
the output needs to be this: "The letter A occurs 3 times
The letter B occurs 2 times
The letter C occurs 1 times
The letter D occurs 4 times
The letter E occurs 5 times
The letter F occurs 2 times
The letter G occurs 1 times
The letter J occurs 1 times
The letter K occurs 1 times
The letter T occurs 4 times
The letter W occurs 1 times"

here is my code so far
Code:
#include <iostream>
#include <cctype>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main (){

int a=0;
int i=0;
for(i=0; myStr1[i] !='\0'; i++){
if (isalpha (myStr1[i])){
myStr2[a]=myStr1[i];
a++;

}
}
myStr2[a]='\0';
cout  << myStr2 << endl;

return 0;
}
i need to get rid of myStr2 because the char needs to be const, so after that all i have done is eliminate all spaces and punctuation and just filter out the letters. I don't know what i should write in the function, and then what i need to write in the main function.

again i looked at the prevoius advice that was given, and the code as well, but i am not familair with any of that, i am on a very basic level, i'm really looking for some help, this is an extra credit assignment as well that would really help out my grade

7. Just use std::map if you are allowed to. Once you get the string comparison down the rest of the work is done for you.

8. i don't know what a std::map is .. i don't know how it works or how to use it

9. yea but i never learned that in my class .. i need to use only things i've learned .. i believe get.line will be part of this program

10. I don't see where mystr2 is even defined. That code isn't compiling, is it?

You need to figure out how to convert the character myStr1[i] into an index a. It's basically converting the character code for 'a' into the number 0 (or 1 if you prefer 1 for some reason). I think it is safe to assume that the characters 'a' through 'z' will all be in order, so you can use that information for this problem.

Focus on that part of the task.

11. daved, mystr2 isn't declared .. and that program doenst compile. the reason its in there is because originally i was going to index a new string with just the alpha characters, but i found out i can only use one const char ..

i'm really confused on these 2 points:

1. what to write in my void funciton
2. what to write in my main function

and i don't know if this is any help but i know how to index one string to another, but i don't know how to index letters to a number and then have them counted

12. >> but i found out i can only use one const char
What do you mean by this? Can you give an example. It doesn't sound correct to me. You can have more than one string. Your second array does not have to be a parameter, it can be declared inside the function.

In this case, myStr2 isn't a string at all, assuming that it is the array you are using to count the number of times a character is found. It should be an array that holds counts and counts are not characters, they are numbers.

Now, as for your questions, you need to figure out what code to put in the void count(const char myStr[]) and what not to, right? Well, look at the signature of the function. It takes only a string. That has to be the input string, right? So the code inside count has to go through the entire string and count the characters. As far as main goes, you just need to call the count function. Your assignment gives an example of how to do that. I don't think you need anything else in there, all the important code goes inside count.

>> but i don't know how to index letters to a number
You figure this out. It is not that hard. There are lots of hints in this thread.

13. what i meant for myStr2 was that .. visual studio wont let me convert a const char to char .. so i couldnt index mystr to mystr2

14. this is what i have ... where do i got from here?!?!? guys if you could be a little more straightforward it would help me a ton .. all i do is get more confused when i hear a bunch of idfferent wayt on how to do things without ever seeing how to do it

Code:
#include <iostream>
#include <string>
using namespace std;

int main (){

int count[256];

for(int i=0; myStr[i] !='\0'; i++){
if (isalpha (myStr[i]))
cout <<  myStr[i];

}
/*
for(int i=0; myStr[i] !='\0'; i++)
cout << "the letter: "<< i << " occured " << count[i] << " times"<< endl;

*/

return 0;

}