Concatenate two strings using pointers
C program to concatenate two strings using pointers.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char str1[MAX], str2[MAX];
char *ptr1, *ptr2;
printf("Enter first string\t:");
gets(str1);
printf("Enter second string\t:");
gets(str2);
ptr1 = str1;
ptr2 = str2;
while(*ptr1 != '\0')
{
ptr1++;
}
while(*ptr2 != '\0')
{
*ptr1 = *ptr2;
ptr1++;
ptr2++;
}
*ptr1 = '\0'; //Makes sure that the string is NULL terminated
printf("\nConcatenated string\t:%s",str1);
getch();
}
Output
Enter first string :welcome to course
Enter second string :crux.com
Concatenated string :welcome to coursecrux.com
Explanation
In the above program, we have to concatenate two strings i.e. we have to append second string at the end of first string.
For this, two strings are taken as an input from the user and stored in the variable named say, 'str1' and 'str2'. Two pointers 'ptr1' and 'ptr2' are taken that points to 'str1' and 'str2' respectively. 'ptr1' and 'ptr2' points at the first character of their respective strings using the statement
We have to append 'str2' at the end of 'str1'. For this, first we have to traverse the pointer 'ptr1' so that it points at the end of the 'str1'.
Then we will copy 'str2' to 'str1' character by character using the below code.
Here, 'ptr1' points at the end of the string 'str1' and 'ptr2' points at beginning of 'str2'. As, we have to append all the characters of 'str2', so loop will be executed till we reach the end of 'str2'
i.e.
character of 'str2' is copied in 'str1'. and both the pointers are incremented by 1, so that they point to the next positions. As soon as the end of string 'str2' is reached, loop terminates. Then, make sure that the concatenated string 'str1' is NULL terminated.
In C programming NULL is represented using 0.
Hence, the above two NULL checking conditions in while loop is unnecessary and can be stripped. So, the program in more geeky way in "Method 2" tab.
For this, two strings are taken as an input from the user and stored in the variable named say, 'str1' and 'str2'. Two pointers 'ptr1' and 'ptr2' are taken that points to 'str1' and 'str2' respectively. 'ptr1' and 'ptr2' points at the first character of their respective strings using the statement
ptr1 = str1;
ptr2 = str2;
We have to append 'str2' at the end of 'str1'. For this, first we have to traverse the pointer 'ptr1' so that it points at the end of the 'str1'.
while(*ptr1 != '\0')
{
ptr1++;
}
Then we will copy 'str2' to 'str1' character by character using the below code.
while(*ptr2 != '\0')
{
*ptr1 = *ptr2;
ptr1++;
ptr2++;
}
Here, 'ptr1' points at the end of the string 'str1' and 'ptr2' points at beginning of 'str2'. As, we have to append all the characters of 'str2', so loop will be executed till we reach the end of 'str2'
i.e.
*ptr2 != '\0'
character of 'str2' is copied in 'str1'. and both the pointers are incremented by 1, so that they point to the next positions. As soon as the end of string 'str2' is reached, loop terminates. Then, make sure that the concatenated string 'str1' is NULL terminated.
*ptr1 = '\0';
In C programming NULL is represented using 0.
Hence, the above two NULL checking conditions in while loop is unnecessary and can be stripped. So, the program in more geeky way in "Method 2" tab.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char str1[MAX], str2[MAX];
char *ptr1, *ptr2;
printf("Enter first string\t:");
gets(str1);
printf("Enter second string\t:");
gets(str2);
ptr1 = str1;
ptr2 = str2;
while(*(++ptr1));
while(*(ptr1++) = *(ptr2++));
printf("\nConcatenated string\t:%s",str1);
getch();
}
Output
Enter first string :welcome to course
Enter second string :crux.com
Concatenated string :welcome to coursecrux.com