Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

String Concatenation

1) Using library function strcat()

The function strcat() is used to join two strings.

Syntax

char *strcat(char *s1, const char *s2);

Example

#include<stdio.h>
#include<string.h>
int main()
{
    char string1[20] = "John";
    char string2[20] = " Smith";
    printf("\n Before Concatenation :: ");
    printf("\n String 1 = %s",string1);
    printf("\n String 2 = %s",string2);
    strcat(string1,string2);
    printf("\n\n After Concatenation :: ");
    printf("\n String 1 = %s",string1);
    printf("\n String 2 = %s",string2);
    return 0;
}

Output

Before Concatenation ::
String 1 = John
String 2 = Smith

After Concatenation ::
String 1 = John Smith
String 2 = Smith

2) Without using library function

Example

#include<stdio.h>
int main()
{
    int x=0,y=0;
    char string1[20]="Have a Nice";
    char string2[20]=" Day";
	
    printf("\n Before Concatenation :: ");
    printf("\n String 1 = %s",string1);
    printf("\n String 2 = %s",string2);
	
    while(string1[x]!='\0')
    {
        x++;
    }
    while((string1[x]=string2[y])!='\0')
    {
        x++;
        y++;
    }
	
    printf("\n\n After Concatenation :: ");
    printf("\n String 1 = %s",string1);
    printf("\n String 2 = %s",string2);
    return 0;
}

Output

Before Concatenation ::
String 1 = Have a Nice
String 2 = Day

After Concatenation ::
String 1 = Have a Nice Day
String 2 = Day

 

Leave a reply