Concatenate Two Character Arrays

Concatenate Two Character Arrays” is a basic operation of character array. Here, we are given two Character Array of length ‘n’ and ‘m’. Our task is to concatenate both the Character Array and store the result into resultant Character Array.


Example:

INPUT:
String 1: Helpme
String 2: studybro
OUTPUT:
Resultant String: Helpmestudybro

The steps required to Concatenate Two Character Arrays and store the result into resultant string is as follows:

  1. Create a resultant character array of size same as sum of size of given two character arrays.
  2. Copy the first character array to resultant character array and mark the index.
  3. Copy second character array to resultant character array from the marked index+1.

C++ Program to Concatenate Two Character Arrays is as follows:

/* Program to Concatenate Two Character Arrays */
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    char str1[len],str2[len];  
    cout<<"Enter the first string:\n";  
    cin>>str1;  
    cout<<"Enter the second string:\n";  
    cin>>str2;  
    int len1 = strlen(str1);  
    int len2 = strlen(str2);  
    int length = len1 + len2 + 1;  
    char result[length];  
    int index = 0;  
    for(int i = 0; i < len1; i++)  
    {  
        result[index] = str1[i];  
        index++;  
    }  
    for(int i = 0; i < len2; i++)  
    {  
        result[index] = str2[i];  
        index++;  
    }  
    result[index] = '
 /* Program to Concatenate Two Character Arrays */ #include<bits/stdc++.h> using namespace std; int main() { char str1[len],str2[len]; cout<<"Enter the first string:\n"; cin>>str1; cout<<"Enter the second string:\n"; cin>>str2; int len1 = strlen(str1); int len2 = strlen(str2); int length = len1 + len2 + 1; char result[length]; int index = 0; for(int i = 0; i < len1; i++) { result[index] = str1[i]; index++; } for(int i = 0; i < len2; i++) { result[index] = str2[i]; index++; } result[index] = '\0'; cout<<"\nThe Resultant string is: "<<result; } 
'; cout<<"\nThe Resultant string is: "<<result; }
OUTPUT:
Enter the first string: helpme
Enter the second string: studybro
The Resultant string is: helpmestudybro
Time Complexity:
O(n), where n is the length of resultant string.

Related Posts:

  1. Program to Check whether given String is Palindrome or not.
  2. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  3. Program to count total number of words in a string.
  4. Program to find smallest and largest word of the String.
  5. Program to find Most Frequent Character of the String.
  6. Program to Remove all the blank spaces from the String.
  7. Program to check if String is isogram or not.
  8. Program to Reverse Each word of the String.
  9. Program to Print All the Substring of the Given String.
  10. Program to find longest palindromic Substring.
  11. Program to check Anagram Strings.
  12. Program to check whether two Strings are Rotation of each other or not.
  13. Program to check Palindromic Anagram.
  14. Program to print all the Palindromic Substring of the String.
  15. Program to check Panagram String.
  16. Program to find first non-repeating character of the String.
  17. Program to find equilibrium index of an Array.
  18. Program to find majority element of an Array.
  19. Program to find mean of the Array.
  20. Program to sort an Array of 0s and 1s.

You may also like...