Using the gets() function, read two strings entered as gets(s1) and gets(s2).
Using the string library function strlen(s1), get the length of the string s1 and set it to j.
The for loop iterates across the for(i=0;s2[i]!='\0′;i++) structure. Append the characters of string s2 to the string s1[i+j] until there are no more characters in the string s2. The string s2 is appended to the end of the string s1.
Print the s1 string, which has been concatenated.
Implementation
C
C
#include<stdio.h> #include <string.h>
int main() { char string1[1000], string2[1000]; int i,j; printf("Enter string number 1: "); gets(string1); printf("Enter string number 2: "); gets(string2); j=strlen(string1);
for(i=0;string2[i]!='\0';i++) { string1[i+j]=string2[i]; } string1[i+j]='\0'; printf("combined two strings ='%s'\n",string1);
Here strcat(s1,s2) is a string library function found in the "string.h" header file.
The strcat(s1,s2) function joins the strings s2 and s1.
Parameters
Description
s1
It is the destination string to which the s2 string is to be attached.
s2
It is the source string that is going to attach to the destination one i.e s1.
It works as strcat() function of string concatenation is called here which takes two arguments or say parameters first are the destination and the second one is the source like the destination is the string after which the source string has to be attached.
Implementation
C
C
#include<stdio.h> #include <string.h>
int main() { char string1[1000],string2[1000]; printf("Enter string1: "); gets(string1); printf("Enter string2: "); gets(string2); strcat(string1,string2); printf("combined two strings ='%s'\n",string1);
The time complexity of concatenating two strings using a loop is O(n + m), where n is the length of the first string and m is the length of the second string.
Concatenate Two Strings Using Pointer
Algorithm
Declare two character pointers str1 and str2.
Move the str1 pointer to the null terminator (\0) by incrementing it until *str1 becomes 0.
Use a loop to copy the characters from str2 to the position pointed to by str1.
After copying all characters, append the null terminator (\0) to str1.
Implementation
C
C
#include <stdio.h>
void concatenate(char *str1, char *str2) { // Move the pointer to the end of str1 while (*str1) { str1++; }
// Append str2 to str1 while (*str2) { *str1 = *str2; str1++; str2++; }
When manually concatenating two strings without using strcat(), the time complexity remains O(n + m).
Frequently Asked Questions
How to print two strings in C?
You can use the printf() function to print two strings by passing both strings as arguments. For example: printf("%s %s", str1, str2);.
How to compare two strings in C?
You can use the strcmp() function to compare two strings. It returns 0 if the strings are equal, a positive value if the first is greater, and a negative value if the second is greater.
How to concatenate string pointers in C?
To concatenate string pointers in C, move the first pointer to its null terminator, then copy characters from the second string using a loop or strcat() function.
Conclusion
In this article, we see the implementation of the C program to concatenate two strings by various methods. We had also seen the output of the written program on some random input.
If you want to learn more about C programs, visit the given links below: