I have this project where I have push characters onto a stack from phrase until either one of the characters in encrypt1 is encountered that's also in the the phase. Then, it is supposed to stop and pop and print off the characters in the stack until it gets to one of the characters in encrypt2 that's next in the phrase. I get the jist of what I have to do, but I'm having problems thinking of the best way to combine the if statements with the for loops.

So far I've tried combining all of the phrases into one for loop with multiple if, else if's but I think it would be out of bounds since the encrypt1 and encrypt2 strings are always only going to be four characters long and the phrase can be much more then that.

for(c=0;c < phrase.length();c++)
    {
     if (phrase[c] == encrypt1[c])     
      myStack.push(phrase[c]);

Any thoughts? Would a while work here with the for loops? Could I take advantage of the fact that encrypt1 & 2 will always be set at four characters?

thanks

I suspect you could end up with a protocol using one or more nested loops. However, an example of encrypt1, encrypt2, and phrase with expected output would be helpful.

I suspect you could end up with a protocol using one or more nested loops. However, an example of encrypt1, encrypt2, and phrase with expected output would be helpful.

Yes, it would.

LOL

encrypt1 = GOOD

encrypt2 = LUCK

phrase = SOUNDS SIMPLE TO ME

output = OSDNOT EEM LPMIS SU

Any thoughts? Would a while work here with the for loops? Could I take advantage of the fact that encrypt1 & 2 will always be set at four characters?

You could, but shouldn't. If they throw a different length encrypt phrase at you, the code breaks.

You'll need 2 for loops (and another while loop) inside the main while loop. The first to check each char of the phrase to see if it is in the encrypt1 phrase. If so, the second for loop will check each "popped" char from the stack to see if it is in encrypt2 (there will be a while loop to read the chars from the stack):

while chars in phrase
    for chars in encrypt1
        is encrypt.char equal phrase.char
            while chars in stack
                for chars in encrypt2
                    is stack.char equal encrypt2 char

Obviously, you'll need to put in the checks to know when to put chars onto the stack and when to stop popping chars off the stack - exercise left to the requester! :)

looks like each char in phrase is pushed onto the stack until a char in e1 is encountered. The trigger char in e1 is printed immediately and then everything is popped from the stack until the stack is empty or a char from e2 is found. If a char from e2 is found it is remains on the stack (or in this case is pushed back on to the stack immediately after being popped off) and popping from the stack is stopped.

Here's pseudocode for how I might go. WARNING: This pseudocode is an example. It has not be tested.

for each char in phrase
    set flag1 to false
    for each char in encrypt1
       if phraseChar equal encrypt1Char
          flag1 is true
          break out of this loop

    if flag1 is false
          push phraseChar onto stack
    if flag1 is true
          print encrypt1Char
          flag2 is false
          while(stack not empty and flag2 is false)
              pop stack and assign it's value to poppedChar
              set flag1 to false
              for each char in encrypt2
                  if poppedChar equals encrypt2
                     flag1 is true
                     break out of this loop

              if flag1 is true
                 push poppedChar back onto stack
                 flag2 is true
             if flag1 is false
                 print poppedChar

Good luck.

Thanks a lot everyone!

Now time for me to get to work...

:)

Hi here, thanks again for the help tho I had two more questions.

When you say in your pseudocode code

for each char in phrase

do you mean something like...

for (a=0;a < phrase.length();a++)

????

Also, when you add the line

break out of this loop

since its a for loop, will it not just break out of the loop by itself after it gets to the end of phrase or in the case of its a while loop until whatever condition listed is met or is that just something related to it being pseudocode ?

thx

>>for (a=0;a < phrase.length();a++)

Yes, though I prefer
int length = phrase.length();
for(a - 0; a < length; ++a)
so phrase.length() isn't called every time through the loop (as long as the length remains constant for the entire loop process)

>>break out of this loop

You don't have to break out of the loop early if you don't want to. It's more efficient if you do, but it's not necessary. As long as the flag remains true through the rest of the loop once a match to encrypt1 has been found you should be okay.

I kinda came up with this nested for structure based on your guys examples. It's not working correctly though, as output to the two encryption phrases of GOOD & LUCK and the main phrase of SOUNDS SIMPLE TO ME, I get
O
S
S
S
S
The stack is empty.

Is this because it stays "stuck" inside the inner most c loop until its finished bypassing b and a? So I'll probably have to go a step further and, similar to your code, and add some flags and conditions to fix the order or execution.

do
    {
    for (a=0;a < phrase.length();a++)
    {
       for (b=0;b < encrypt1.length();b++)
           {
           if (phrase[a] != encrypt1[b])
           myStack.push(phrase[a]);
          
           else
           cout<<myStack.pop()<<endl;
                 for (c=0;c < encrypt2.length();c++)
                      {
                        if (phrase[a] == encrypt1[c])
                        cout<<myStack.pop()<<endl;
                      }
            }
    }
    }
    while (phrase.length() != 0);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.