Thread: Counting occurances of two letters in a particular order?

  1. #16
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    @Char*Pntr: Your code MAY break on the phrase "Mississippi is since I was", not on just Mississippi.

    Edit: Oh wait one! You changed it - sly devil! Try the trouble phrase, above. Do you get 3?
    I did not change anything... but you changed the "trouble phrase"

    I typed in the "trouble phrase" in my code, and, um I got (sob!) I got 4.

    The correct result would be 3. So you're right, my code has a bug in it... but
    holy moley (sp.?) , I never would have expected a crazy input like that.

    How in the world do you know about "trouble phrases?"

    That is pretty cool. It's getting late here... I glimpsed at your code, by tomorrow
    I wont remember it. I'll get my pen and paper out, and use your trouble phrase
    as an input.

    But once that bug is fixed, what do you suggest to change the code to a
    "state machine" implementation?

    btw, if the OP had typed in that trouble phrase, I would have fixed that bug. I only
    spent 5 mins writing this code.. and spent hours on this thread Grrr..!

    But it was all worthwhile, as you taught me many important things.

    It's late here.... thanks Adak!

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    A state machine would look something like this :

    Code:
    #define STATE_LOOKING_FOR_I      1
    #define STATE_FOUND_I 2
    
       int state = STATE_LOOKING_FOR_I;
       while ((ch = getchar()) != '#')
       {
          switch(state)
          {
             case STATE_LOOKING_FOR_I:
                /* Move to FOUND_I state if input is an i */
                if (ch == 'i')
                   state = STATE_FOUND_I;
                break;
             case STATE_FOUND_I:
                if (ch == 's')
                   count += 1;
                state = STATE_LOOKING_FOR_I;
                break;
             default:
                printf("Error : bad state\n");
                exit(0);
          }
       }
    Total overkill for this small of a problem, but imagine you're looking for a longer string. Each character found in the correct order would lead to the next state in the sequence until you get to the final one where you add 1 to the count. At any point along the way if you see the wrong character you just go back to the first state and start over. No need to mess with multiple flags for each character and lots of nested if statements, just a simple check in each intermediate state.

    You could make it work with arbitrary strings; something like (untested code but you get the idea):

    Code:
    const char sequence[] = "looking";
    int state = 0;
    
    while ((ch = getchar()) != '#')
    {
        if (ch == sequence[state]) /* found next character in sequence */
        {
            if (state == (strlen(sequence) - 1)) /* matched all the way to end of sequence */
            {
                count += 1;
                state = 0;
            }
            else
                state += 1; /* check next char in sequence */
        }
        else
            state = 0; /* start over from beginning of sequence */
    }

  3. #18
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thank you everyone again for the help!! I am working hard to learn this and I know its is simple stuff for most of you.

    Char*Pntr I did try working it out on paper a few times to get my logic down. No excuses for me but its been the mid 80's since I had a Basic programming class.

    Adak - Thank you!!!! I have been using the debugger line by line watching what my code was doing but just couldn't get over the hump. I knew what it needed to do. Your solution makes perfect sense now. I am learning!

    final code:
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char ch;
        int is_count = 0;
        int lastchar = 0;
    
        printf("Key in your word or phrase:  \n");
        
        while ((ch = getchar()) !='#')
        
        {
            if (ch == 'i')
                lastchar = 1;
            else {
                    if (ch != 's')
                        lastchar = 0;
                }
            
            
            if (ch == 's' && lastchar == 1)
            {
                is_count++;
                lastchar = 0;
            }
        }
        lastchar = 0;
        printf("\"is\" count %d\n", is_count);
    
        getchar();
        getchar();
        return 0;
    }

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by d2rover
    final code:
    The logic looks correct, but you could improve your indentation. Also, I notice that you are assigning to lastchar on almost every iteration. This suggests a simplification:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int current;
        int previous = '\0';
        int is_count = 0;
    
        puts("Key in your word or phrase:  ");
    
        while ((current = getchar()) != '#' && current != EOF)
        {
            if (current == 's' && previous == 'i')
            {
                is_count++;
            }
            previous = current;
        }
    
        printf("\"is\" count %d\n", is_count);
    
        getchar();
        getchar();
        return 0;
    }
    Notice that I declared ch as an int since getchar returns an int, and you should do this in order to check against EOF (just in case).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warnings about initializer order
    By CodeMonkey in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2009, 07:55 PM
  2. Switching letters
    By XiReDDeViLiX in forum Windows Programming
    Replies: 4
    Last Post: 06-06-2002, 06:48 AM
  3. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM
  4. Counting letters in window
    By Soldier in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 11:34 PM
  5. Replies: 1
    Last Post: 01-06-2002, 06:28 PM