Adsense 728 * 15

Search

Custom Search

C, C++ Program to Concatenate two Strings

Write a C, C++ program to concatenate two strings.


Logic of concatenating two strings.

i) Declare two character array and take input from user.

ii)  Traverse first string to reach at the end, now start concatenating second string into first.

Interview Questions on String.


C, C++ Program to Concatenate two Strings


#include<stdio.h>
#include<conio.h>


int main(){
  
  int i=0,j=0;
  
  /* Declare character array string1 and string2. */

  char string1[100],string2[100]; 


  puts("Enter first string");
  gets(string1);

  puts("\nEnter second string");
  gets(string2);
  
  /* Reach at the end of first string. */

  while(string1[i]!='\0'){     
      i++;
  }

 
 /* Concatenate second string .*/

  while(string2[j]!='\0'){              
      string1[i++]=string2[j++];
  }

  string1[i]='\0';

  printf("After concatenation the string is \n");
  puts(string1);

  return 0;

}
Output:

Enter first string :  cquestions

Enter second string : .in

After concatenation the string is : cquestions.in

No comments:

Post a Comment