how to make a string from single characters

I have array of 5 letters

array[1]="h";
array[2]="e";
array[3]="l";
array[4]="l";
array[5]="o";

how do I put it into one string

string str="hello";

Check for: which one of the following is syntactically and semantically correct and then re-post or amend your post.

array[5] = “o”;
array[5] = ‘o’;

GolamMostafa has a valid point. Single quotes (') for single characters, double quotes (") are for strings of characters.

Arrays are indexed from 0, not 1.

The difference between a character array and a string or null terminated character array is the null terminator ('\0').

string str="hello";

There is no data type called string. Resist the temptation to use String objects. Their use can lead to memory corruption.

char array[6];

void setup()
{
   array[0] = 'h';
   array[1] = 'e';
   array[2] = 'l';
   array[3] = 'l';
   array[4] = 'o';
   array[5] = '\0';  // null terminator makes the character array into a string.
   
   Serial.begin(115200);
   Serial.print("the array contains  ");
   Serial.print(array);
}

void loop()
{

}

Cool! So simple and pleasant solution.

groundFungus:
GolamMostafa has a valid point. Single quotes (') for single characters, double quotes (") are for strings of characters.

Arrays are indexed from 0, not 1.

The difference between a character array and a string or null terminated character array is the null terminator ('\0').

string str="hello";

There is no data type called string. Resist the temptation to use String objects. Their use can lead to memory corruption.

char array[6];

void setup()
{
  array[0] = 'h';
  array[1] = 'e';
  array[2] = 'l';
  array[3] = 'l';
  array[4] = 'o';
  array[5] = '\0';  // null terminator makes the character array into a string.
 
  Serial.begin(115200);
  Serial.print("the array contains  ");
  Serial.print(array);
}

void loop()
{

}

that you can print character one after another using print everybody knows

but how do you

print

String str = "hello";
Serial.println(str);

where you first made the word hello from those 5 characters in array, this was my question how to make that string not to print something which looks like string

The added '/0' makes array a string. That was the point of the example code. You can copy, search, do comparisons using the Cstring functions, not just print it.

Do you know the difference between a String and a string. You use the terms interchangeably.

char array[6]="hello";
String str = String (array) ;

I think this should answer your question. Keep in mind what was said before, String - s tend to corrupt memory on long term use

My personal favorite is option #2 for building a string in an array (listed below):

char array_[8];
void setup() {
  Serial.begin(115200);
}

void loop() {
  option1();
  option2();
  option3();

}

void option1() {
  memset(array_, '\0', sizeof(array_)); //clear array
  array_[0] = 'h';
  array_[1] = 'e';
  array_[2] = 'l';
  array_[3] = 'l';
  array_[4] = 'o';
  array_[5] = '-';
  array_[6] = '1';
  array_[7] = '\0';  // null terminator makes the character array into a string.
  String str;
  for (int i = 0; i < sizeof(array_); i++) {
    str = str + array_[i];
  }
  Serial.println(str);
  delay(1000);
}


void option2() {
  memset(array_, '\0', sizeof(array_)); //clear array
  strcat(array_, "h"); //place "h" in first available slot
  strcat(array_, "e"); //place "e" in first available slot
  strcat(array_, "l");  //place "l" in first available slot
  strcat(array_, "l");  //place "l" in first available slot
  strcat(array_, "o"); //place "o" in first available slot
  strcat(array_, "-");  //place "-" in first available slot
  strcat(array_, "2"); //place "2" in first available slot

  String str;
  str = String(array_);

  Serial.println(str);
  delay(1000);
}

void option3() {
  memset(array_, '\0', sizeof(array_)); //clear array
  strcpy(array_, "hello-3");  //store String in char array
  String str;
  str = String(array_);

  Serial.println(str);
  delay(1000);
  
}

My personal favorite is option #2 for building a string in an array

Why do they all use the String data type when it is already in a string ?

groundFungus:
The added '/0' makes array a string. That was the point of the example code. You can copy, search, do comparisons using the Cstring functions, not just print it.

Do you know the difference between a String and a string. You use the terms interchangeably.

You probably meant '\0'.

UKHeliBob:
Why do they all use the String data type when it is already in a string ?

UKHeliBob, you are absolutely correct. My example was subtle - to display a method of concatenating strings should the OP desire to do at a later date.

memset(array_, '\0', sizeof(array_)); //clear array
strcat(array_, "h");
strcat(array_, "ello");
strcat(array_, "-2");

String str;
str = String(array_);

Serial.println(str);

You probably meant '\0'.

Yes, thanks for the correction.

My example was subtle - to display a method of concatenating strings should the OP desire to do at a later date.

Concatenating the strings is fine, but why did you turn the resulting string into a String when it could have been printed without the conversion ?

UKHeliBob:
Concatenating the strings is fine, but why did you turn the resulting string into a String when it could have been printed without the conversion ?

Oh I see. Regarding the OP's intial post, I came to the same conclusion as lesept in post #6.

JMeller:
Oh I see. Regarding the OP's intial post, I came to the same conclusion as lesept in post #6.

But his solution came with a warning

Keep in mind what was said before, String - s tend to corrupt memory on long term use

UKHeliBob:
But his solution came with a warning

Yes sir, you are correct again. Surely one can recognize the subtle nudge in options #2 and #3 to transition from the String class. I was hesitant to do so until peers like yourself pointed me to easy solutions to do so.

I typically use a String and set the size of the String by using the .reserve() method, such as with this code:

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == '>')
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == '<' )
          {
            sSerial = ""; // clear string buffer
            BeginSentence = true; // found beginning of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fParseSerial( void * parameters  )

By using the reserve method the space the string occupies is set and memory fragmentation does not create issues. sSerial = “”; is used to clear the String buffer, .concat ( OneChar ) is used to add to the String buffer, and there are String other methods available to produce desired results.

A ProMini project has now been running over a year without the need for reset/reboot that makes use of the .reserve() method.

Idahowalker:
I typically use a String and set the size of the String by using the .reserve() method, such as with this code:

bookmarked for further study; +1