Ropes Data Structure ( Fast String Concatenation ) | Simple C ++ program to concatenate two strings ; Function that concatenates strings a [ 0. . n1 - 1 ] and b [ 0. . n2 - 1 ] and stores the result in c [ ] ; Copy characters of A [ ] to C [ ] ; Copy characters of B [ ] ; Driver code ; Concatenate a [ ] and b [ ] and store result in c [ ]
#include <iostream> NEW_LINE using namespace std ; void concatenate ( char a [ ] , char b [ ] , char c [ ] , int n1 , int n2 ) { int i ; for ( i = 0 ; i < n1 ; i ++ ) c [ i ] = a [ i ] ; for ( int j = 0 ; j < n2 ; j ++ ) c [ i ++ ] = b [ j ] ; c [ i ] = ' \0' ; } int main ( ) { char a [ ] = " Hi ▁ This ▁ is ▁ geeksforgeeks . ▁ " ; int n1 = sizeof ( a ) / sizeof ( a [ 0 ] ) ; char b [ ] = " You ▁ are ▁ welcome ▁ here . " ; int n2 = sizeof ( b ) / sizeof ( b [ 0 ] ) ; char c [ n1 + n2 - 1 ] ; concatenate ( a , b , c , n1 , n2 ) ; for ( int i = 0 ; i < n1 + n2 - 1 ; i ++ ) cout << c [ i ] ; return 0 ; }