Replacing string Characters

For some quick background I'm working on maze solving robot. I have four different characters for each action

L-Left turn
R- Right Turn
T- Turn Around
S- Straight pass a turn

But in order to make the robot solve the maze faster I need to substitute some steps. For example: Left, Turn Around, and Left could be substituted by going straight past a turn, thereby getting rid of that dead end.

With Strings there was a nice replace String.replace function, but I was warned of the dangers of Strings. So I switched over to strings (the null character stuff, got to love that naming scheme!). So I don't know how to go about shortening the maze code wise.

I think I could check every three moves to see if something happened, but I don't know how replace them with the simplified version.

This also seems a bit brute force, needing to go through around 10ish if statements, so I'm open to Ideas!

if (Maze[Step-3] == 'L' && Maze[Step-2] =='T' && Maze[Step-1] == 'L'){

?then?

}

This is the Code I'm using right now to mess around with strings

char* Maze[] = {};

int Step = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {

  Maze[Step] = 'L';
  Step++;
  Serial.print("L");
  delay(500);

  Maze[Step] = 'R';
  Step++;
  Serial.print("R");
  delay(500);

  Maze[Step] = 'T';
  Step++;
  Serial.print("T");
  delay(500);

  Maze[Step] = 'S';
  Step++;
  Serial.print("S");
  delay(500);
}

Thanks a Ton!

Maybe define another code called "skip the next two moves"? Then overwrite the first character of the set of 3 moves with that one.

How can I overwrite the characters? Could you give me some example code? Thanks!

Let's say the code for the "Skip" action was 'K'. If you wanted to overwrite Maze[Step] with that value, you just do this:

Maze[Step] = 'K';

Huh That could work for some of the substitutions, but in the sequence LTL skipping two wouldn't get me a straight. Is there any way I can move the cursor back and just delete things?

Watson221:
Huh That could work for some of the substitutions, but in the sequence LTL skipping two wouldn't get me a straight. Is there any way I can move the cursor back and just delete things?

It would be far easier to give you advice if you posted examples of what the input and output should be.

If you have a hand of cards, say JQA3481, and wanted to replace the A34 sequence with a 7, you'd put the cards you wanted to keep in one place, add the new card (the 7), discard the cards you don't want, and place, one by one, the rest of the cards in your hand onto the new hand you are creating.

Copying some elements from one array to another is simply a matter of choosing which elements to copy, and where to copy them to.

Maybe this little sketch would be useful. It does something much like you're wanting.

// https://forum.arduino.cc/index.php?topic=593691.msg4034858#msg4034858
// original name: LookForAccents

// Locates and converts text characters with diacritical
// marks to plain text.

void diacriticalStripper (char Text[], int TextLength) {
  int i = 0;
  do {
    char c = Text[i];
    if ((uint8_t)c == 195) { // diacritical code?      
      char c2 = Text[i + 1]; // yes, convert next character
      switch ((uint8_t)c2) {
        case 160 ... 166:
         Text[i] = 'a';
          break;
        case 167:
          Text[i] = 'c';
          break;
        case 168 ... 171:
          Text[i] = 'e';
          break;
        case 172 ... 175:
          Text[i] = 'i';
          break;
        case 177:
          Text[i] = 'n';
          break;
        case 178 ... 182:
          Text[i] = 'o';
          break;
        case 185 ... 188:
          Text[i] = 'u';
          break;
        case 189 ... 191:
          Text[i] = 'y';
          break;
        case 128 ... 134:
          Text[i] = 'A';
          break;
        case 135:
          Text[i] = 'C';
          break;
        case 136 ... 139:
          Text[i] = 'E';
          break;
        case 140 ... 143:
          Text[i] = 'I';
          break;
        case 146 ... 150:
          Text[i] = 'O';
          break;
        case 153 ... 156:
          Text[i] = 'U';
          break;
        default:
          break;
      }
      for (int j = i + 1; j < TextLength; j++) Text[j] = Text[j + 1];
      TextLength --;
    }
    i++;
  } while (i < TextLength);
}

void setup() {
  Serial.begin(115200);
  Serial.print("Remove diacriticals\n\n");
  char texte[] = "àaâäcçeèé12ëïîioöoôûuUüùÂA34ÄÊ5EË6ÎÏ7ÔÖ8UÛÜ9ÀÇÉÈÊ0Ô";
// char texte[] = "àaâäc";
  Serial.println(texte);
  diacriticalStripper (texte, strlen(texte));
  Serial.println(texte);
}

void loop() {}

Paul,
I have some of them worked out, but I'm still making a list of more.

LTR = T
RTL = T
LTS = R
STL = R
STS = T

DougP, Is this little snippet of code the deleting part?
TextLength --

@Watson221, do not cross-post. Other thread removed.

Watson221:
DougP, Is this little snippet of code the deleting part?
TextLength --

Yes. Deletion is accomplished by moving the remaining characters down one place, thus overwriting the diacritical code for the current character - after correcting the character code.

Watson221:
Paul,
I have some of them worked out, but I'm still making a list of more.

LTR = T
RTL = T
LTS = R
STL = R
STS = T

DougP, Is this little snippet of code the deleting part?
TextLength --

Where does this string come from? Is it literally a fixed string like:

const char *Maze = "LRSTLLRST...";

From where is this sourced?

BlackFin,
The string comes from the maze path. If the robot turns left then an L is added to the string. If it turns around a T is added and so on.

I'm also having difficulty getting a serial print or really any representation of what the Maze's map is in real time.

I get 0⸮ when I use

Serial.print(Maze[Step]);

or 

Serial.print(Maze[Step++]);

Is there an other way I can do this?

I added this bit of code, but it's still not deleting and substituting.

Serial.print (Step);
 
  if (Maze[3] == 'P') {
    Serial.print("Happy");

  }

}

void ShortMaze() {

  if (Maze[Step - 3] == 'R' && Maze[Step - 2] == 'T' && Maze[Step - 1] == 'S') {
    Step--;
    Maze[Step] = 'P';
  }

}

By adding in the Serial.print Step It only prints 3. I think this means that the ShortMaze (); isn't subtracting so that Step--; bit isn't working. Any other methods?

You should take your snippets to the fine folks at http://snippets-r-us.com. I'm sure that they'll get you straightened out in no time.

The array is like a long row of boxes. Imagine you have different colored balls in your boxes. You pull one out, replace it with a different color. That's easy.

After that replacement you want to take out the two balls after that. Also easy. But now you have two empty boxes. How do you move all the boxes to the right of that point to the left by 2 steps?

Start with the 3rd position after you replaced. Move that one 2 boxes left. Go to the 4th. Move that 2 boxes left.

The tricky part is when you get to the end. The Arduino is stupid. It doesn't "feel" balls when it picks up. So it will keep moving right and grabbing empty air and moving that left. It doesn't even feel the boxes so it can easily move off the end of the array and move other parts of memory you dont own.

Morgan S,

I'm getting a bit lost in the metaphor. Especially this part

Start with the 3rd position after you replaced. Move that one 2 boxes left. Go to the 4th. Move that 2 boxes left.

From what I understand the program is deleting things, but the cursor isn't moving back.

So If I had

1 2 3 4 5

I want to delete 3 4 5 and replace it with 6

So First I delete 3 4 5

1 2_ _ _

Then I move the cursor to 2 (cause strings start at 0 right?) and add 6

1 2 6 _ _

That still leaves me with 2 blank spots, so do I add a fake/ nonreactive character? Like N for nothing?

1 2 6 N N

So First I delete 3 4 5

No. You REPLACE the first character with the new character, so you have 1, 2, 6, 4, 5, 7.

Then, you move all the characters after the one you don't want up two positions, so you have 1, 2, 6, 7.

Some time with paper and pencil would be time well spent. Put the computer away for a while.

Then, you move all the characters after the one you don't want up two positions

I don't understand why moving the character up would erase them from the string. Wouldn't that just change the position and not the whether is exists? Or do I have to move it to the end before I can erase it?

I tried to work this out before I replied to Morgan S, but I'm still having trouble understanding the erasing and moving part. As in I don't understand why you have to move the all the boxes to the right of that point to the left by 2 steps.

I don't understand why moving the character up would erase them from the string

Suppose you have a string containing 1234567. You want to replace 345 with Z.

You replace the 3 with a Z. Now you have 12Z4567.

Next, you move the 6 (the first character you want to keep) up two places. Now, you have 12Z6567.

Next, you move the 7 (the next character you want to keep) up two places. Now, you have 12Z6767.

Finally, you the NULL up two places. Now, you have 12Z67. Yes, there is still stuff after the NULL, but that doesn't matter.

Like I said, paper and pencil will teach you a lot.

Ok thanks, That really helped me grasp the concept!

But I have a few clarifying questions,

Finally, you the NULL up two places. Now, you have 12Z67. Yes, there is still stuff after the NULL, but that doesn't matter.

So that would be 1 2 Z 6 7 \0 6 7 hereby deleting that last 6 7?
Also adding that null value makes it so I can't add anything afterwards, right?