# counting letter occurences in a string

Show 80 post(s) from this thread on one page
Page 3 of 3 First 123
• 05-04-2008
Daved
You tell us how you plan on solving the problem. Yes, there are lots of different hints here, but if you understand them, then it is not too confusing.

You have a start. You are outputting the characters in the string that are alphabetic characters.

These are the steps I would follow:

1. Create an empty void count(const char myStr[]) function that does nothing. Compile and make sure the code compiles.
2. Call that function from main the way the assignment demonstrates. Make sure that compiles.
3. Take your code that you have now that checks isalpha and add it to the count function. Make sure that compiles. Then run it and make sure you get something you expect.
4. Now comes the harder part. How do you define the count array. First, don't call it count, because the name of the function has to be count (according to the assignment). So give it a different name. In your current code you made it an array of int. That is good, because the counts will be ints. You specified 256 as the size. That can work but is not the only way. Do you know why you picked 256?
5. The answer to the previous question will tell you what to do here. Instead of using cout to output the character at myStr[i] you need to increment the proper count. How you do it depends on how you intend to solve the problem. Work on understanding your solution in English (not code), and if you get stuck then ask about that part here.
• 05-04-2008
pjr5043
ok i have this code .. i just want to change one thing, instead of entering a string .. i'd like to have this be the default string "ABcaBadd*eekjdfefdeg,TTew44Tt"

Code:

#include <iostream>
#include <cctype>
using namespace std;

//counts how many each character shows

void count(const char myStr[]){
for (int x = 97; x < 123; x++){
int count1 = 0;
for (int i =0; myStr[i] != '\0' ; i++){
if (myStr[i] == (char)x){
count1++;
}
}
if (count1 != 0)
cout << (char)x << " appears " << count1 << " times\n";
}
}

int main(){

char phrase1[500];

cout << "What is your String?\n";
cin.getline (phrase1, 500);

const int x=500;
char myarray[x];

int c = 0;
//takes each char from the string and check to see if it is both
// not a space and is a alphabetical char
for(int i = 0; phrase1[i] != '\0'; i++){
if (isalpha(phrase1[i])){
myarray[c]=tolower(phrase1[i]);
c++;
}
}
//reassigns null terminator to the end of the array
myarray[c]= '\0';

count(myarray);

return 0;
}

• 05-04-2008
Daved
You can initialize phrase1 with whatever you want and skip the input part.

BTW, what is 97 and 123?
• 05-05-2008
maverix
Quote:

Originally Posted by pjr5043

//counts how many each character shows
void count(const char myStr[]){
for (int x = 97; x < 123; x++){
int count1 = 0;
for (int i =0; myStr[i] != '\0' ; i++){
if (myStr[i] == (char)x){
count1++;
}
}
if (count1 != 0)
cout << (char)x << " appears " << count1 << " times\n";
}
}
[/CODE]

This can be done in a better way!!!
Code:

void count(const char myStr[]){
declare an array of 26 integers to store the count of each alphabet

initialize the array variables to zero

loop until you reach the end of myStr[]
if current variable in myStr[] is an alphabet
increment the corresponding index in the integer array

loop through the integer array and print the values in each index
}

cheers
maverix
• 05-05-2008
pjr5043
thank you for your help .. and daved the 97 and 123 was so taht it only counts 26 times .. the amount of letter that are in the alphabet i guess, i recieved that from another student because i was having so much trouble with the program
• 05-05-2008
Daved
Thanks. I figured you didn't write the code but was hoping you understood what the number meant. (Why not 96 and 122 or 0 and 26?) In fact 'a' would normally be better than 97, but for this assignment it doesn't matter.
Show 80 post(s) from this thread on one page
Page 3 of 3 First 123