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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    11
    Create a bool such as: lastCharWas and set it to false initially. Then when you find the first character you are looking for set it to true. Then when looking for the next character, if 'lastCharWas' is true, and the next character is whatever, increment the count. Then set 'lastCharWas' to false.

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Quote Originally Posted by Brokenhope View Post
    Create a bool such as: lastCharWas and set it to false initially. Then when you find the first character you are looking for set it to true. Then when looking for the next character, if 'lastCharWas' is true, and the next character is whatever, increment the count. Then set 'lastCharWas' to false.
    Thanks for the tip. I couldnt get the bool function to work so i used int set to 0 or 1. Here is my code now but its just counting the "s". Am I barking up the right tree?

    Off to find dinner. I'll check back after my belly quits complaining!
    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;
    		
    		if (ch == 's' && lastchar == 1)
    			is_count++;
    			
    	}
    	lastchar = 0;
    	printf("\"is\" count %d\n", is_count);
    
    	getchar();
    	getchar();
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Every time you find an 's', (which has been preceded by an i), you need to reset a variable.
    Last edited by Adak; 09-27-2010 at 11:42 PM.

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Adak View Post
    Every time you find an 's', you need to reset a variable.
    like this:

    Code:
    			is_s_count = FALSE;
    		}
    
    		is_s_count = FALSE;
    my god, I never thought this thread would last this long. Probably never broke out
    the pen & paper like I originally suggested.

    And I still don't see that 3rd if() to test if both are "i" and "s" were set. I have the
    working solution for this, but I require some thought and effort by the OP.

    Adak, can I IM you my code? I tested it and it seems to work fine. I'm not sure if it meets
    the "state machine" implementation, that may/may not be required.

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