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

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
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];
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 = strcat(string1, string2);
printf(“\nAfter concatenation:”);
//prints the contents of string whose address is in finalstr
printf(“\n finalstr = %s”, finalstr);
//prints the contents of string1
printf(“\n string1 = %s”, string1);
//prints the contents of string2
printf(“\n string2 = %s”, string2);
return 0;
Before concatenation:
string1 = first
string2 = second
After concatenation:
finalstr = firstsecond
string1 = firstsecond
string2 = second
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().

This entry was posted in C,C++ Programing, Freshers, Programing, Technical Interviews Questions and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>