String concatenation in C programming language using strcat & pointers. This is to concatenate the given input strings as one. It is a simple concatenation source code that generates the final output.
Here we have included the 2 example programs. They are,
- String concatenation using strcat,
- String concatenation using pointers
[Read: Software Company List] & [Aptitude online portal]
STRING CONCATENATION IN C
This C program is used to concatenate the two string values into the third string and print the value of string and combined two strings by using the strcat function & pointers. The header file is #include<string.h>. For example first value is ‘string’ and second value is ‘concatenation’ after strcat it will concatenate and stored in third value as ‘string concatenation’.
String concatenation codes:
1) C program for string concatenation using strcat function.
//Header files
#include <stdio.h>
#include <string.h>
int main()
{
//Program variable
char b[50], c[50];
printf("Enter the first value of string\n");//Display function
gets(b);//Getting input function
printf("Enter the second value of string\n");
gets(b);
strcat(b,c);//concatenation function
printf("After string concatenation is %s\n",b);
return 0;
}
2) Program in string concatenation using pointers
//Header files
#include<stdio.h>
#include<conio.h>
char *xs(char*,char*);//Global variable
void main()
{
char *s4,*s5,*s6;//Program variable
clrscr();
puts("\t enter the first value of string:\n");
gets(s4);
puts("\t enter the second value of string:");
gets(s5);
s6=xs(s4,s5);
printf("after concatenation of string is %s",s6);
getch();
}
char *xs(char *s1,char *s2)
{
char *p=s1;
/*int j,h=0;
while(*(s1+h)!='\0')
{
h++;
}
j=h;
h=0;
while(*(s2+h)!='\0')
{
*(s1+j)=*(s2+h);
h++;
j++;
} */
while(*s1!='\0')
s2++;
while(*s1!='\0')
{
*s1=*s2;
s1++;
s2++;
}
*s4='\0';
//fs4--;
return (p);
}