String concatenation in C,C program to concatenate two string using pointer.
In this tutorial we will learn how to write a c program that concatenate two strings by use of pointer.
We will enter two string like first string is - Hello and second string is - Friends,
then the output concatenated string is - Hello Friends
In this tutorial we will learn how to write a c program that concatenate two strings by use of pointer.
We will enter two string like first string is - Hello and second string is - Friends,
then the output concatenated string is - Hello Friends
C PROGRAM CODE
#include<stdio.h>
main()
{
char s1[25],s2[25],*t1,*t2;
int len1=0,len2=0,i;
printf("\n ENTER FIRST STRING:\n ");
scanf("%s",&s1);
printf("\n ENTER SECOND STRING:\n ");
scanf("%s",&s2);
t1=s1;
//copying base address of string
t2=s2;
while(*t1!='\0')
{
t1++;
len1++;
}
//copying second string to first
while(*t2!='\0')
{
*t1=*t2;
t1++;
t2++;
len2++;
}
*t1='\0';
i=0;
//taking total string to initial base address
while(i<len1+len2)
{
i++;
t1--;
}
printf("\n CONCATENATED STRING:\n ");
while(*t1!='\0')
{
printf("%c",*t1);
t1++;
}
printf("\n ");
}
OUTPUT:
Output of concatenated string |
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment