string manipulation not working as expected...

the sketch:

char bigString[30] = "Partly%20Cloudy";
//char *ptrBig = bigString;
char littleString[30] = "%20";

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
  Serial.println(cleanString(bigString,littleString));
  Serial.println("end");
}

void loop()
{

}

char *cleanString(char *targetString, char *searchString)
{
  char* stringChunk;
  char buffer[30];
  char *bufferPtr = buffer;
  stringChunk = strstr(targetString, searchString);
  targetString[stringChunk - targetString] = '\0';
  Serial.print(targetString);
  Serial.print("+");
  Serial.print(stringChunk + strlen(searchString));
  Serial.println("=");
  Serial.println(" ");
  bufferPtr = strcat(" ", stringChunk + strlen(searchString));
  //Serial.println(" ");
  return (strcat(targetString, bufferPtr));
}

outputs as:

start Partly+Cloudy=

Partly Cloudy end

uncomment the Serial.print() in the function and it outputs this:

start Partly+Cloudy=

Cloudy Partly Cloudy end

and I am tired and stumped...

Your strcat() is screwed up. it strcat(src,dst) takes dst and apppends it to the end of src. Your src in your code is the constant string " ".

  bufferPtr = strcat(" ", stringChunk + strlen(searchString));

Your strcat() is trying to append your string to a two byte string constant. That is going to overwrite some memory and cause unexpected results.

Perhaps you meant:

strncpy(buffer, targetString, sizeof buffer);
  strncat(buffer, " ", sizeof buffer);
  strncat(buffer, stringChunk + strlen(searchString), sizeof buffer);
  return buffer;  // Note: 'buffer' should be global or declared 'static'

Using the "strn..." helps prevent buffer overflows.

You should check that the string you are cleaning actually contains the search string before you use the returned value as a character pointer.

thanks John,

I put the test for the presence of littleString in the function call...

char bigString[30] = "Partly%20Sunny%20Skies";
char littleString[30] = "%20";

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
  char *ptr = strstr(bigString, littleString);
  if (ptr)
  {
    Serial.println(cleanString(bigString,littleString));
  }
  Serial.println("end");
}

void loop()
{

}

char *cleanString(char *targetString, char *searchString)
{
  static char buffer[30];
  while(strstr(targetString, searchString))
  {
    char* stringChunk;
    stringChunk = strstr(targetString, searchString);
    targetString[stringChunk - targetString] = '\0';
    strncpy(buffer, targetString, sizeof buffer);
    strncat(buffer, " ", sizeof buffer);
    strncat(buffer, stringChunk + strlen(searchString), sizeof buffer);
    strncpy(targetString, buffer, sizeof buffer);
  }
  return buffer;  // Note: 'buffer' should be global or declared 'static'
}

This line has problems

 targetString[stringChunk - targetString] = '\0';

I think this notation should follow the form of

char*[int - int] = whatever

But you have

char* [char* - char*] = whatever

I don't think that's going to work.

KenF: I don't think that's going to work.

but, it does...

Am I missing something, or in my example code does it just happen to work?

Try this:

char bigString[30] = "Partly%20Sunny%20Skies";
char littleString[30] = "%20";
char buffer[30];

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
  if (strstr(bigString, littleString))
  {
    cleanString(bigString,littleString);
    strncpy(bigString, buffer, sizeof bigString);
    Serial.println(bigString);
  }
  Serial.println("end");
}

void loop()
{

}

char *cleanString(char *targetString, char *searchString)
{
  while(strstr(targetString, searchString))
  {
    char* stringChunk;
    stringChunk = strstr(targetString, searchString);
    targetString[stringChunk - targetString] = '\0';
    strncpy(buffer, targetString, sizeof buffer);
    strncat(buffer, " ", sizeof buffer);
    strncat(buffer, stringChunk + strlen(searchString), sizeof buffer);
    strncpy(targetString, buffer, sizeof buffer);
  }
  return buffer;
}

but, it does…

Ah I see what you’re doing now. You’re subtracting one pointer from the other to get the offset into the char arrray. (my mistake).

KenF: Ah I see what you're doing now. You're subtracting one pointer from the other to get the offset into the char arrray. (my mistake).

Yes... trying to focus my effort on processing C strings and learning pointers. This was my approach to parsing some messages sent with HTTP commands.

fun stuff.