Pages

Friday

Program to Concatenate the Given Two Strings in C

Here is a C program to concatenate two strings using C function strcon(function). This program uses C concepts like For Loop in C, Auto Incrementing Operator ++ and Auto Decrementing Operator --.


# include <stdio.h>
# include <conio.h>
void main()
{
char str1[20], str2[20], strcon[40] ;
int i, j ;
clrscr() ;
printf("Enter the first string : ") ;
scanf("%s", str1) ;
printf("\nEnter the second string : ") ;
scanf("%s", str2) ;
for(i = 0 ; str1[i] != '\0' ; i++)
strcon[i] = str1[i] ;
i-- ;
for(j = 0 ; str2[j] != '\0' ; j++)
strcon[i+j+1] = str2[j] ;
strcon[i+j+1] = '\0' ;
printf("\nThe concatenation string is : %s", strcon) ;
getch() ;
}


Output of above program

Enter the first string : Awesome
Enter the second string : Weather
The concatenation string is : AwesomeWeather

Take a Look at: Examples of different operations on Strings

1 comment:

  1. The strcat function concatenates a source string to destination string and adds a null character at the end of destination. Source string is not modified in this operation. It returns concatenated destination string. Length of destination string must be more than sum of original length of strings.

    ReplyDelete