Friday, December 7

Concatenate Two Strings


Write a C program to concatenate two strings without using strcat() function.

strcat(string1,string2) is a C standard function declared in the header file string.h
The strcat() function concatenates string2, string1 and returns string1.

Program: Program to concatenate two strings

#include<stdio.h>
#include<string.h>
char *strct(char *c1, char *c2);
char *strct(char *c1, char *c2) 
{
        // strlen function returns length of argument string
int i = strlen(c1);
int k = 0;
        // loops until null is encountered and appends string c2 to c1
while (c2[k] != '\0') 
{
c1[i + k] = c2[k];
k++;
}
return c1;
}
int main() 
{
char string1[15] = "first";
char string2[15] = "second";
char *finalstr;
printf("Before concatenation:" " \n string1 = %s \n string2 = %s", string1, string2);

//addresses of string1, string2 are passed to strct()
finalstr = strct(string1, string2);
printf("\nAfter concatenation:");

//prints the contents of string whose address is in finalstr
printf("\n finalstr = %s", finalstr);

printf("\n string1 = %s", string1);     // prints the contents of string1

printf("\n string2 = %s", string2);    // prints the contents of string2
return 0;
}

Output:
Before concatenation:
string1 = first
string2 = second
After concatenation:
finalstr = firstsecond
string1 = firstsecond
string2 = second

Explanation:
string2 is appended at the end of string1 and contents of string2 are unchanged. In strct() function, using a for loop, all the characters of string 'c2' are copied at the end of c1. return (c1) is equivalent to return &c1[0] and it returns the base address of 'c1'. 'finalstr' stores that address returned by the function strct().