Algorithm
Concatenation of two strings is simple copying a string to another. To concatenate two strings str1 and str2, you will copy all characters of str2 at the end of str1.
Below is the step by step descriptive logic to concatenate two string.
Input: Two strings, str1 and str2
Output: Concatenated string result
1. Declare two character arrays to store the strings: str1[100], str2[100] (adjust the size based on your requirements).
2. Input the first string from the user and store it in str1.
3. Input the second string from the user and store it in str2.
4. Use the strcat() function from the string.h library to concatenate str2 to the end of str1.
a. Example: `strcat(str1, str2);`
5. Display the concatenated string result to the user.
Code Examples
#1 Code Example- C Programing to concatenate two strings using while loop
Code -
C Programming
/**
* C program to concatenate two strings
*/
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
int i, j;
/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
/* Move till the end of str1 */
i=0;
while(str1[i] != '\0')
{
i++;
}
/* Copy str2 to str1 */
j = 0;
while(str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}
// Make sure that str1 is NULL terminated
str1[i] = '\0';
printf("Concatenated string = %s", str1);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Input string2: DevSEnv
Output
#2 Code Example- C Programing to concatenate two strings
Code -
C Programming
/**
* C program to concatenate two strings
*/
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
int i, j;
/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
/* Move till the end of str1 */
i=-1;
while(str1[++i]);
/* Copy str2 to str1 */
j = 0;
while(str1[i++] = str2[j++]);
printf("Concatenated string = %s", str1);
return 0;
}
Copy The Code &
Try With Live Editor
#3 Code Example- C Programing to concatenate two strings using pointer
Code -
C Programming
/**
* C program to concatenate two strings using pointer
*/
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
char * s1 = str1;
char * s2 = str2;
/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
/* Move till the end of str1 */
while(*(++s1));
/* Copy str2 to str1 */
while(*(s1++) = *(s2++));
printf("Concatenated string = %s", str1);
return 0;
}
Copy The Code &
Try With Live Editor
#4 Code Example- C Programing to concatenate two strings using strcat() function
Code -
C Programming
/**
* C program to concatenate two strings using strcat()
*/
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum size of the string
int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
/* Concatenate str1 with str2 */
strcat(str1, str2);
printf("Concatenated string = %s", str1);
return 0;
}
Copy The Code &
Try With Live Editor
Output
Enter second string: DevsEnv
Concatenated string = I love DevsEnv
Demonstration
C Programing Example to Concatenate Two Strings-DevsEnv