Thread: counting letter occurences in a string

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  2. #17
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    this is pretty well covered in your anagram program thread.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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. #19
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote 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. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    alrite .. i'll have to look at them an analyze them more

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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 (){
    
    	const char myStr[]="ABcaBadd*eekjdfefdeg,TTew44Tt";
    
    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. #22
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    i don't know what a std::map is .. i don't know how it works or how to use it

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.
    Last edited by Daved; 05-04-2008 at 05:03 PM.

  12. #27
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  14. #29
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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 (){
    	
    	const char myStr[]="ABcaBadd*eekjdfefdeg,TTew44Tt";
    	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;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM