How to duplicate each characters in an input array

Hello,I need help with my assignment. I’m just a beginner in Arduino coding.
I have problem with my coding which is duplicating each character of an input array.
For example:
Input : Good morning
Output: GGoooodd mmoorrnniinngg

Can someone please correct me if I’m wrong and show me the correct way :slight_smile:
and also does anybody knows how to use strtok function correctly?
this is my code

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(20);

}

void loop() {
// put your main code here, to run repeatedly:
int i;
char s[100];
char *b = s;
int j;
int limite;
int n;

while(Serial.available()){
s[i++]=Serial.read();
delay(1);
}
s*=0;*

  • if(i>0){*

  • }*

  • b = strtok(s,":");*

  • Serial.print(s);{*

  • }*

  • for(int j=0; j<limite;j++){*
    b[j] += s + s*;*
    * Serial.print(b); *

* }*
}
sketch_jun20a.ino (489 Bytes)

What does it do, or not do?

In serial monitor, when you give an input( exp: Hello), then it will print out its output of (HHeelloo) but I couldn't figure out the function for the duplication . This is not apply to int or float.

This should produce the effect you want:

void setup()
{
  Serial.begin(115200);
}


void loop()
{
  if (Serial.available())
  {
    char c = Serial.read();
    Serial.write(c);
    Serial.write(c);
  }
}

dave_croft: In serial monitor, when you give an input( exp: Hello), then it will print out its output of (HHeelloo) but I couldn't figure out the function for the duplication . This is not apply to int or float.

I'm not asking what the assignment says it should do, I'm asking what your program actually does when you run it. Is it a requirement of the assignment that you finish typing a line before it feeds the characters back?

Yes exactly

Can you then, please answer my question?

My program supposedly read a variable character of array for 100 (max length) when you type it in and in this case I convert it to token using strtok but Im not sure if that's the correct way of using it. Then for array start with position 0, it'll duplicate or add the same array next to it until the final position of array.i hope I answer your question :) Im sorry for not quite understanding your question

I would think it’s a lot easier to place every character read in from serial into two successive array elements, rather than duplicate them in the array after the fact. Or not bother with an array at all, as per john’s example.

Yeah,John example works just fine and easy, thank you But I think it'll be better for me if I know the alternate way of introducing a function that could duplicate the array so that I will understand much better in managing it and could tackles other problem involving array in my assignment :)

dave_croft: My program supposedly read a variable character of array for 100 (max length) when you type it in and in this case I convert it to token using strtok but Im not sure if that's the correct way of using it. Then for array start with position 0, it'll duplicate or add the same array next to it until the final position of array.i hope I answer your question :) Im sorry for not quite understanding your question

My question, can you tell me when you type [whatever you send] to the program, it sends back [what?]. I'm assuming that you've tried compiling it... in other words, how does your program actually behave currently (... and please don't say, "it behaves well"...). Does it run? Can you post the output from it here?

Here's what I got, when I type in hello, it doesn't gives me the output.

Input: hello

It seems that the program doesn't read my coding for the duplication and doesn't print the output that I wanted. That's what I'm missing.

dave_croft: It seems that the program doesn't read my coding for the duplication and doesn't print the output that I wanted. That's what I'm missing.

Imagine you have 100 pigeon-holes each of which can only store a single character:

char s[100];

Now imagine I gave you a photocopier which can duplicate one of the characters from a pigeon hole and replace the character in another pigeon-hole:

 s[...] = s[...];

Write down the individual steps (algorithm) you would need to perform that assignment manually.

Things to think about... 1. Would you start duplicating at the first pigeon hole, or the last one? 2. If you start at the first one, where are you going to put the copied character if you can’t put more than one character in any one pigeon-hole? 3. What happens when all 100 pigeon-holes are already used before you start? 4. Having more than half of them used is a bit of a problem, isn’t it? 5. Would it help any if I gave you another set of pigeon-holes d[200]?

Edit: This won’t work...

b[j] += s + s;

Or at least not the way you think it does. With a character array it is not possible to put more than one character in any single index (individual pigeon-hole).

char * DoubleString(char *buffer, const char *s)
{
  for (size_t i=0; i <= strlen(s); i++)  // INCLUDE the terminating NULL character
  {
    buffer[i*2] = s[i];
    buffer[i*2+1] = s[i];
  }
  return buffer;
}


void setup()
{
  Serial.begin(115200);
}


void loop()
{
  char inputCharacters[101];
  char doubleBuffer[202];
  
  if (Serial.available())
  {
    size_t count = Serial.readBytesUntil('\n', inputCharacters, 100);
    inputCharacters[count] = '\0'; // Terminating null
    Serial.write(DoubleString(doubleBuffer, inputCharacters));
  }
}

Why store each character twice?
Why not buffer and then simply print each array element twice?
Remember - RAM is precious

TheMemberFormerlyKnownAsAWOL: Why store each character twice? Why not buffer and then simply print each array element twice? Remember - RAM is precious

You'd have to ask the teacher that assigned the homework. My guess is that it has something to do with learning about array indexes.

Of course we have not seen the actual assignment, just a description of one person's understanding of the assignment. It's possible that my first suggestion was sufficient.