Big Programming challenge for functions in arduino

Hi, I have been using Arrays and functions for a while to manipulate strings. I am really really struggling because I want to create a function which can take a String variable(not Array of chars) as an argument/Parameter and manipulate it using arrays or something(Not any inbuilt Function to modify string) then return the modified version of string to any variable if called. [PROBLEM] - it is that I tried everything but couldn't solve this issue, I tried referencing, copying, even tried to find how were actual functions like strupr() were initially coded in first place, but nothing worked. Is there any way to do exactly what I want? if not what's the similar approach? I don't want to pass entire array in function but just a String variable and it should return a modified string value. Problem arises when I try to think about how do I put a string variable's value as elements in an array using functions. Here's my code:

int A[] = {1, 3, 4, 7, 5 ,8, 9, 10};
String K = "hello";

String *ptr = &K;
void setup(){
  Serial.begin(115200);
}


void loop(){
  Serial.println(Func(K));
}

String Func(String N){

  //String gets modified here for eg: - We can remove a specific char in the string using //loops etc but not any inbuilt function to do that.
  return N;
}

Please some help me I really need this. I will really appreciate any help for the solution. :slight_smile:

Is the character array A simply a distraction?
What about ptr?

When you pass a string (lowercase s) to a function then you are actually passing a pointer to it rather than the string itself

Can you please clarify why you want to pass a String rather than a string and exactly what data type you want to pass back ?

what are you asking? what is the problem?
if you change N in Func it prints it changed

String Func(String N){

  //String gets modified here for eg: - We can remove a specific char in the string using //loops etc but not any inbuilt function to do that.
  return N + "?" ;  // this modifies the String by adding "?" to the end.
}

However, to fulfil the requirements of this assignment, your are anyway going to have to unload the String into a character array (or at least get a pointer to it), manipulate it as specified ( say remove the third character as an example) using non-String functions, then repack it as a String. I doubt if you can directly and reliably alter the internal representation of a String, especially if you want to start adding characters to it.

You should really post an example where all details are shown what you want to do.

C++ does
##not
offer straight-forward string manipulation in a sense of

String myString1 = "Hello World";
String myString2;

// function
String KeepFirstWord(String myStringParameter)
// code to cut out first word

myString2 = KeepFirstWord(myString1);

What makes most sense depends on the kind of string-manipulations you want to do
And this is the reason why you should post an examples that shows in all detailed

your string before manipulation
Your wished result after the string-manipulation.

Example creating uppercase

input: "hello"

result: "HELLO"

or cut out each second character

input: 123456789
result: 13579

best regards Stefan

I'm sort of guessing that the assignment is to form a String out of only the character in the original String which are a positions in array A[] (if these exist). So "hello" would end up as "hll" (index starts at 1).

Yes that A[] is pretty much a distraction. I was experimenting things forgot to remove that,

So I want to pass a string in a function's argument like Function(word) and then I want to do some modifications to the string like remove a specific character and then return or pass back it to a variable so it would :- Strvar = Function(word); so if I put word = "Hello" then I should be able to pass back modified version as "Hllo". Strvar = Hllow that's what I want. I want function to take in the string as an parameter and then put it in inside an array inside that function then do some manipulation with that then return the string. I can't figure out after passing a string into function how do i put that string into an array.
I couldn't understand what do you mean by String and string.

Yes you're absolutely right. I wanted to somehow put that string as individual elements in that array A[]. Well I can do this simply by doing A[] = "hello" but I want to do this using function so that if I pass string K into function "Func()" like Func(K) I want this function to put that string into an array inside the function.

Oh I was looking for straight forward solution, like exactly the way you did it in your example. I think there should be a solution for this because for example : many functions like strupr() they do exactly like what you did straight forwardly. I wanted to make my own string manipulation functions according to needs for eg I need a function that can detect any character in a string such that Detect('') it should detect if '' is present in string and if it is then return true otherwise false

No it's just a simply empty function I wanted to pass string K in that function Func and then I wanted this Func to store that string K into an array as an individual elements and then I can use that array in loop to do anything I want, like detect anything or remove or make it uprcase or lwrcase or just verify if the string exists in my 2Darray database.

I think yes I can alter string reliably only thing I am stuck at is how to unload a string into an char Array inside a function so that each index has individual characters of that string. Do you know how to do it? I thought pointers can do something like that but idk how to use pointers I started to learn about them but then I thought maybe I should learn how does memory work to understand pointers. It's been 3 days and still can't understand pointers

Have you looked at the source code for the String function toCharArray?

I think I haven't where is the source code?

source code for a string to char array right? using function?

You should be aware that String and string are different things.
string is tha array of char with\0 at the end.
String is s class supposed to make life easier. But if you want to do build your own manipulation function it will be more difficult
So either use strings or convert your String to a char array in your function as suggested... And then maybe back to return a String.

in short:

  1. use the SafeString-library instead of variable-type String.

  2. either with String or with SafeString assigning a variable a manipulated character-sequence is done through placing the variable

inside

the parameter list

myStringManipulatingFunction(myResultStr, myInputStr);

There is a fundamental difference between variable type "string" and variable-type String.

Can you see the difference "string" written with a lower-case "s" and

String written with a CAPITAL "S" (=UPPERCASE)

using the variable type String on a microcontroller-system which has very limited RAM is a dangerous thing.
If you assign a String new values thousands of times this will eat up all memory and will make your program crash or malfunction because RAM occupied by other variables will become overwritten. This will cause very weird behaviour and very hard to find bugs.
You can read about it here

You did

not

yet understand that in C++ Strings are accessed via pointers.
and this is the reason why it is

not

possible to have a function that returns Strings in the way you want it

does

not

work!

This is like trying to ride a usual bike over water
simply does not work!

It does work in a different way
let's say you have a String

String myFirstStr = "Hello"
and you want to remove the "e" and have the result stored in a second String

String myResultStr;

what you can do is coding a function that works this way
void myCharRemover(String &resultStr,String inputStr, char removeCh)

At first
myFirstStr shall contain "Hello"

the character "e" shall be removed
so the result will be "Hllo" which shall be stored inside the variable myResultStr

and this is done this way
myCharRemover(myResultStr,myFirstStr,'e');

the variable is placed inside the parenthesises

myResultStr

is placed

inside

myCharRemover(myResultStr,myFirstStr,'e');

If you are unwilling to accept this fact you should change to a different programming-language like Delphi, Python etc.

From what you have posted so far and what your questions are I can conclude that you are not yet a profound expert about C++-programming.
As long as you aren't such an expert you should stay away from using variabletype String (the one with the capital "S")

I recommend using the SafeString-library. It offers most of the comfort that variable-type String does but as its names says it is safe to use and will never cause memory-problems.

again: if you insist on having a function with result-type String in this way

myResultStr = myCharRemoverFunction(InputStr..
change to different programming-language like Delphi or python

inside Arduino-IDE C++ having it this way
myResultStr = myCharRemoverFunction(InputStr..

not possible

best regards Stefan

This lack of understanding is at the core of the problem

You previously said

but never said why not use a zero terminated array of chars, ie a string rather than a String