Thread: finding most common char in a string

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    finding most common char in a string

    I need to find the character that occurs the most in a string (not a char array). The user enters a string, and from there I thought I could use scanf, but I didn't find anything about scanf that says it could return the most used character. (ie. if someone enters "this is my str" then S would be the most used char).

    Is there a function I'm not aware of that can do this, a better way of doing it, or not possible at all?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use a map<char, int> to map chars to their occurrences. Insert the chars into the map and increment the occurrence for that. When you are done with the insertions, look for the element in the map with the highest occurrence.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use an array with as many elements as there are characters to count.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Alright, i tried Salem's idea, but it always comes out with 'L' as being the most used char even if there isn't an L in the string. I think it might have to do with turning the string to a char array so I can read each char seperately. I switched the L char to a different letter and it displayed that char. I also took the 'L' out, and typed "ttttt" and it said 'a' was the most common. So is it the strcpy() screwing it up?

    Code:
    char ReturnMostUsed(string* ptr){
    
        char common;
        int size = sizeof(ptr), count = 0;
        char line[size+1];
        int a=0,b=0,c=0,d=0,e=0,f=0,g=0,
            h=0,i=0,j=0,k=0,l=0,m=0,n=0,
            o=0,p=0,q=0,r=0,s=0,t=0,u=0,
            v=0,w=0,x=0,y=0,z=0;
    
        strcpy(line,(char*)ptr);
    
        while(count <= size){
    
            if(isalpha(line[count])){
                if(line[count] == 'a')
                    a++;
    // further down the code it goes through all the possible chars
    // then finds which ever count is highest, then displays that char

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You didn't understand the idea. Look here:
    Code:
    int chars[255];
    ZeroMemory(chars, 255);
    char* str = "aaabbbcccddd...zz";
    for (int i = 0; i < strlen(str); i++)
    	chars[str[i]]++;
    Work from that.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I'm not understanding this part...
    Code:
    chars[str[i]]++;
    What's the purpose of putting the 'char* str' inside the 'int chars' index? More effectively, what does that accomplish by doing that?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because char is basically an int. str[i] returns an int, an int that represents the number of the character at that location in the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. need help on finding char in string [newbie]
    By gtr_s15 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2005, 04:28 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM