# Big Programming challenge for functions in arduino

**URL:** <https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635>\
**Category:** Programming\
**Created:** [December 29, 2021, 8:18am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635 "2021-12-29T08:18:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 29, 2021, 8:18am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/1 "2021-12-29T08:18:09Z")

</div>

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:

```cpp
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;
}

```

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 29, 2021, 8:21am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/2 "2021-12-29T08:21:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [December 29, 2021, 8:24am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/3 "2021-12-29T08:24:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 29, 2021, 8:27am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/4 "2021-12-29T08:27:39Z")

</div>

> [@cyphonvoid](#):
>
> I don't want to pass entire array in function but just a String variable and it should return a modified string value.

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 ?

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [December 29, 2021, 10:00am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/5 "2021-12-29T10:00:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [December 29, 2021, 10:18am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/6 "2021-12-29T10:18:46Z")

</div>

```cpp
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.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [December 29, 2021, 12:06pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/7 "2021-12-29T12:06:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [December 29, 2021, 12:16pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/8 "2021-12-29T12:16:43Z")

</div>

> [@cyphonvoid](#):
>
> ```arduino
> int A[] = {1, 3, 4, 7, 5 ,8, 9, 10};
> string K = "hello";
> 
> ```

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).

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:04pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/9 "2021-12-30T23:04:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:11pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/10 "2021-12-30T23:11:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:14pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/11 "2021-12-30T23:14:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:19pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/12 "2021-12-30T23:19:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:23pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/13 "2021-12-30T23:23:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:29pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/14 "2021-12-30T23:29:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [December 30, 2021, 11:35pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/15 "2021-12-30T23:35:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:38pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/16 "2021-12-30T23:38:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cyphonvoid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@cyphonvoid](https://forum.arduino.cc/u/cyphonvoid)\
**Post date:** [December 30, 2021, 11:39pm UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/17 "2021-12-30T23:39:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [December 31, 2021, 5:18am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/18 "2021-12-31T05:18:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [December 31, 2021, 8:33am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/19 "2021-12-31T08:33:43Z")

</div>

in short:

1. use the **SafeString** -library instead of variable-type **S** tring.

2. either with **S** tring 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 **S** tring.

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

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

using the variable type **S** tring 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

> **[The Evils of Arduino Strings](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/)**
>
> Due to Wordpress’s abysmal handling of code blocks this blog post is now hosted at Everyone, when they’re starting out on the Arduino and similar boards, learns to use the String object…

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

> [@cyphonvoid](#):
>
> Strvar = Function(word);

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 **S** tring (the one with the capital "S")

I recommend using the SafeString-library. It offers most of the comfort that variable-type **S** tring 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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 31, 2021, 8:43am UTC](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635/20 "2021-12-31T08:43:09Z")

</div>

> [@cyphonvoid](#):
>
> I couldn't understand what do you mean by String and string.

This lack of understanding is at the core of the problem

You previously said

> [@cyphonvoid](#):
>
> I want to create a function which can take a String variable(not Array of chars)

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

[Next page](https://forum.arduino.cc/t/big-programming-challenge-for-functions-in-arduino/940635.md?page=2)
