OneBite.Dev - Coding blog in a bite size

concatenate a string in C

Code snippet on how to concatenate a string in C

  char myString[255] = "Hello";
  char myNewString[255] = "World";
  char output[255];
 
  strcpy(output, myString);
  strcat(output, myNewString);
 
  printf("%s\n", output);

This code concatenates two strings, “Hello” and “World”, into one string, “HelloWorld”. We start by declaring three char arrays to hold the strings, myString, myNewString, and output. Then, the strcpy function is used to copy the contents of myString into output. Then the strcat function is used to append the contents of myNewString to the end of output. Lastly, the printf function prints out the combined string, “HelloWorld”.

c