Concatenarea a două șiruri în C

Având în vedere două șiruri str1 și str2, sarcina noastră este să concatenăm aceste două șiruri. Există mai multe moduri de a concatena două șiruri de caractere în limbajul C:

  • Fără a utiliza funcția strcat().
    • Folosind metoda standard
    • Utilizarea funcției
    • Folosind recursiunea
  • Folosind funcția strcat().

1. Concatenarea a două șiruri de caractere fără a utiliza funcția strcat().

A. Folosind metoda standard

 Input: str1 = 'hello', str2 = 'world' Output: helloworld Input: str1 = 'Geeks', str2 = 'World' Output: GeeksWorld 

Abordare: Folosind operatorul „+”.

C++




#include> #include> using> namespace> std;> int> main() {> > string str1 => 'Geeks'> ;> > string str2 => 'ForGeeks'> ;> > string result = str1 + str2;> > cout < < result < < endl;> > return> 0;> }>

Ieșire

GeeksForGeeks 

Abordare: Folosind funcția de adăugare.

C++




#include> using> namespace> std;> int> main() {> > string str1 => 'hello'> ;> > string str2 => 'world'> ;> > cout < <> 'The Resultant String Is :'> < cout < return 0; }>

Ieșire

The Resultant String Is : helloworld 

Analiza complexității:

Complexitatea timpului: O(1).

Spațiu auxiliar: O(1).

Abordare:

  • Obțineți cele două șiruri de caractere să fie concatenate
  • Declarați șiruri noi pentru a stoca șirul concatenat
  • Introduceți primul șir în noul șir
  • Introduceți al doilea șir în noul șir
  • Tipăriți șirul concatenat

Mai jos este implementarea abordării de mai sus:

C




// C Program to concatenate two> // strings without using strcat> #include> > int> main()> {> > > // Get the two Strings to be concatenated> > char> str1[100] => 'Geeks'> , str2[100] => 'World'> ;> > > // Declare a new Strings> > // to store the concatenated String> > char> str3[100];> > > int> i = 0, j = 0;> > > printf> (> ' First string: %s'> , str1);> > printf> (> ' Second string: %s'> , str2);> > > // Insert the first string> > // in the new string> > while> (str1[i] !=> ' '> ) {> > str3[j] = str1[i];> > i++;> > j++;> > }> > > // Insert the second string> > // in the new string> > i = 0;> > while> (str2[i] !=> ' '> ) {> > str3[j] = str2[i];> > i++;> > j++;> > }> > str3[j] => ' '> ;> > > // Print the concatenated string> > printf> (> ' Concatenated string: %s'> , str3);> > > return> 0;> }>

C++




// C++ Program to concatenate two> // strings without using strcat> #include> using> namespace> std;> > int> main()> {> > > // Get the two Strings to be concatenated> > char> str1[100] => 'Geeks'> , str2[100] => 'World'> ;> > > // Declare a new Strings> > // to store the concatenated String> > char> str3[100];> > > int> i = 0, j = 0;> > > cout < <> ' First string: '> < < str1;> > cout < <> ' Second string: '> < < str2;> > > // Insert the first string> > // in the new string> > while> (str1[i] !=> ' '> ) {> > str3[j] = str1[i];> > i++;> > j++;> > }> > > // Insert the second string> > // in the new string> > i = 0;> > while> (str2[i] !=> ' '> ) {> > str3[j] = str2[i];> > i++;> > j++;> > }> > str3[j] => ' '> ;> > > // Print the concatenated string> > cout < <> ' Concatenated string: '> < < str3;> > > return> 0;> }> // this code is contributed by shivanisingh>

Ieșire

First string: Geeks Second string: World Concatenated string: GeeksWorld 

Complexitatea timpului: O(m+n)
Spatiu auxiliar: O(1)

B. Utilizarea funcției

Abordare:

  • funcția principală va apela funcția concatenate_string() pentru a concatena două șiruri.
  • Funcția va obține lungimea șirului s cu ajutorul lui strlen.
  • Acum vom adăuga caracterul șirului s1 la s[i+j]. Acest pas va fi repetat până când niciun caracter nu este disponibil în s1. Adăugăm caractere ale șirului s1 la s de la sfârșitul lui s.
  • După bucla for, vom concatena șirul s.
  • La sfârșit, funcția principală va imprima șirul care este concatenat.

C




// C program to concatenating two> // strings using function> #include> #include> void> concatenate_string(> char> * s,> char> * s1)> {> > int> i;> > int> j => strlen> (s);> > for> (i = 0; s1[i] !=> ' '> ; i++) {> > s[i + j] = s1[i];> > }> > s[i + j] => ' '> ;> > return> ;> }> int> main()> {> > char> s[5000], s1[5000];> > printf> (> 'Enter the first string: '> );> > gets> (s);> > printf> (> 'Enter the second string: '> );> > gets> (s1);> > // function concatenate_string> > // called and s and s1 are> > // passed> > concatenate_string(s, s1);> > printf> (> 'Concatenated String is: '%s' '> , s);> > return> 0;> }>

Ieșire:

Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com' 

Complexitatea timpului: O(n+m) , unde n este dimensiunea șirului 1 și, respectiv, m este dimensiunea șirului 2.
Spațiu auxiliar: O(1)

C. Utilizarea recursiunii

Abordare:

  • Funcția concatenate_string() va obține șirurile s și s1.
  • dacă nu sunt prezente elemente în s1, atunci atribuiți s1 un caracter nul ( ).
  • altfel, dacă sunt prezente elemente, vom adăuga elementul șirului s1 la sfârșitul șirului s și vom crește valoarea lui i cu 1.
  • Funcția concatenate_string se va apela prin trecerea șirurilor modificate s, s1 ca argumente. Această funcție se va numi recursiv până când nu sunt disponibile elemente în s1.

C




// C program to concatenate two> // strings with the help of> // recursion> #include> #include> void> concatenate_string(> char> * s,> char> * s1)> {> > static> int> i = 0;> > static> int> j => strlen> (s);> > if> (!s1[i]) {> > s1[i] => ' '> ;> > }> > else> {> > s[i + j] = s1[i];> > i++;> > concatenate_string(s, s1);> > }> }> int> main()> {> > char> s[5] => 'Geeks'> , s1[8] = 'forGeeks;> > // function concatenate_string> > // called and s1 and s2 are> > // passed> > concatenate_string(s, s1);> > printf> (> ' Concatenated String is: '%s' '> , s);> > return> 0;> }>

Ieșire:

Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com' 

Complexitatea timpului: O(n+m) , unde n este dimensiunea șirului 1 și, respectiv, m este dimensiunea șirului 2.
Spațiu auxiliar: O(1)

2. Folosind funcția strcat().

Funcția strcat() din C adaugă copia șirului sursă la destinație cu un caracter Null la sfârșitul șirului. Se află sub fișierul antet string.h în C.

C




// C program to concatenate two> // strings using strcat function> #include> #include> int> main()> {> > char> s[] => 'Geeks'> ;> > char> s1[] => 'forGeeks'> ;> > // concatenating the string> > strcat> (s, s1);> > printf> (> 'Final string is: %s '> , s);> > return> 0;> }>

C++




#include> #include> using> namespace> std;> int> main()> {> > char> s[] => 'Geeks'> ;> > char> s1[] => 'forGeeks'> ;> > // concatenating the string> > strcat> (s, s1);> > cout < <> 'Final string is: '> < < s;> > return> 0;> }> // This code is contributed by Akshay> // Tripathi(akshaytripathi630)>

Ieșire

Final string is: techcodeview.com 

Complexitatea timpului: O(n+m) , unde n este dimensiunea șirului 1 și, respectiv, m este dimensiunea șirului 2.
Spațiu auxiliar: O(1)