OneBite.Dev - Coding blog in a bite size

combine two strings in C

Code snippet on how to combine two strings in C

#include <stdio.h>
#include <string.h>
 
int main()
{
   char str1[15] = "Hello ";
   char str2[15] = "World!";
   strcat(str1, str2);
   printf("%s \n", str1);
   return 0;
}

This code program combines two strings together. First, it includes the standard input/output header file and the string header file. It then declares two variables, str1 and str2 of type character array and assigns static values to the string variables. The strcat function is used to append str2 to the end of str1 so that the string in str2 is appended after the string in str1. The printf statement prints the combined string on the screen. Finally, the program ends with a return statement.

c