Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Arduino Uno

So this is the code I currently have, I have found a code that could possibly help me but I'm not sure how I would implement that code into my own. What I want it to do after it has printed the message sent, is to print the same message but in reverse.
C++
char Message[128] = "";
char MessageInversed[128] = "";

boolean Done;

void setup()
{

// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("<Enter your text>");
}

void loop()
{
Readmessage();
Showmessage();
}

void Readmessage()
{
static int CharCount = 0 ;
char EnterCheck = '\n';
char NextChar;

while (Serial.available() > 0 && Done == false)
{
NextChar = Serial.read();

if (NextChar != EnterCheck)
{
Message[CharCount] = NextChar;
CharCount++;
}
else
{
Message[CharCount] = '\0';
CharCount=0;
Done = true;
}
}

}



void Showmessage()

{

if (Done == true)

{

Serial.print("Your message length is: ");

Serial.println(strlen(Message));

Serial.print("This is your message: ");

Serial.println(Message);

Done = false;

}

}

This is the code I found, Im not sure how to put it into my own code though if that would be to work:

// Recursive C++ program to reverse an array

#include <bits/stdc++.h>

using namespace std;

/* Function to reverse arr[] from start to end*/

void rvereseArray(int arr[], int start, int end)

{

if (start >= end) 

return; 


int temp = arr\[start\]; 

arr\[start\] = arr\[end\]; 

arr\[end\] = temp; 

// Recursive Function calling 

rvereseArray(arr, start + 1, end - 1); 
}


What I have tried:

Googling, but I don't get how to implement it
Posted
Updated 7-Jun-20 7:09am
v2

1 solution

C++
void rvereseArray(int arr[], int start, int end)

So, you need to pass the function an array of characters, plus the offset of the start and end characters. Something like:
C++
char message[] = "This is a message";
rvereseArray(message, 0, strlen(message) - 1);
 
Share this answer
 
Comments
Member 14856122 7-Jun-20 12:18pm    
Where do I put that in my code? Sorry, I'm quite new to this
Richard MacCutchan 7-Jun-20 12:30pm    
Put the rvereseArray function somewhere above your main function. It would probably be a good idea if you studied the basics of the C language before starting your project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900