[SOLVED] Convering char based function to String

Hi there,

Is there a way to convert this function to return String and also accept String as an input value instead of char?

char *rot47(char *s)
{
  char *p = s;
  while (*p) {
    if (*p >= '!' && *p <= 'O')
      *p = ((*p + 47) % 127);
    else if (*p >= 'P' && *p <= '~')
      *p = ((*p - 47) % 127);
    p++;
  }
  return s;
}

Kind regards,
Viktor

Yes.
But why?
Hint

TheMemberFormerlyKnownAsAWOL:
Yes.
But why?

Because the input is a String, so I wish the output to be String too.

To be able to use it like this:

String inval = "ABCDE";
String outval ="";

outval = rot47(inval);

Here is why the use of String class is discouraged with Arduinos with small amounts of RAM. The Evils of Strings.

groundFungus:
Here is why the use of String class is discouraged with Arduinos with small amounts of RAM. The Evils of Strings.

Yes, I know that, but I will use the function in ESP environment.

Did you investigate my hint?
(ESP can use strings too)

TheMemberFormerlyKnownAsAWOL:
Did you investigate my hint?

Yes, I saw the usage of the charAt():

myString.charAt(n)

But how do I implement it?

If I rewrite while (*p) as the equivalentwhile (p[0]) does that give you a clue?

TheMemberFormerlyKnownAsAWOL:
If I rewrite

while (*p)

as the equivalent

while (p[0])

does that give you a clue?

Wait, wait, it's like any array now?!

Well, not exactly, because you’re advancing the start of the “array” each time through the loop, but you don’t have to do that.

TheMemberFormerlyKnownAsAWOL:
Well, not exactly, because you’re advancing the start of the “array” each time through the loop, but you don’t have to do that.

Okay, I’m starting to get to it, but I’m getting some errors…

String rot47(String p)
{
  while (p[0]) {
    if (p >= '!' && p <= 'O')
      p = ((p + 47) % 127);
    else if (p >= 'P' && p <= '~')
      p = ((p - 47) % 127);
    p++;
  }
  return p;
}

, but I'm getting some errors...

Do you want help with those errors? If so, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Also post the latest code so we can keep up.

(ESP can use strings too)

Whilst it can use strings the ESP environment is awash with Strings including the Espressif ESP32 webserver library on which I spent/wasted some time on wondering why it did not work when I used strings to build a web page

UKHeliBob:
Whilst it can use strings the ESP environment is awash with Strings including the Espressif ESP32 webserver library on which I spent/wasted some time on wondering why it did not work when I used strings to build a web page

I'm not using ESP32, I'm on ESP8266 and did some big project with like four A4 pages in HTML + Bootstrap included externally with String, and it's working without any issues.

groundFungus:
Do you want help with those errors? If so, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Also post the latest code so we can keep up.

The error is:

converting to 'const String' from initializer list would use explicit constructor 'String::String(char)'

The latest code is above your post.

Just put a String wrapper around it.

if (p >= '!' && p <= 'O') p is a pointer, remember?

aarg:
Just put a String wrapper around it.

Where to put String wrapper?

TheMemberFormerlyKnownAsAWOL:
if (p >= '!' && p <= 'O') p is a pointer, remember?

Yes, pointer starts with *, I remember, but how to continue?

But a pointer can’t be an ASCII character - you forgot to dereference it.