Twee snaren aaneenschakelen in C

Gegeven twee strings str1 en str2, is het onze taak om deze twee strings aan elkaar te koppelen. Er zijn meerdere manieren om twee tekenreeksen samen te voegen in de C-taal:

  • Zonder de functie strcat() te gebruiken
    • Standaardmethode gebruiken
    • Functie gebruiken
    • Recursie gebruiken
  • De functie strcat() gebruiken

1. Twee strings aaneenschakelen zonder de functie strcat() te gebruiken

A. Gebruik van de standaardmethode

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

Aanpak: gebruik de operator ‘+’

C++




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

Uitvoer

GeeksForGeeks 

Aanpak: gebruik van de append-functie.

C++




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

Uitvoer

The Resultant String Is : helloworld 

Complexiteitsanalyse:

Tijdcomplexiteit: O(1).

Hulpruimte: O(1).

Benadering:

  • Zorg ervoor dat de twee strings worden samengevoegd
  • Declareer nieuwe strings om de samengevoegde string op te slaan
  • Voeg de eerste string in de nieuwe string in
  • Voeg de tweede string in de nieuwe string in
  • Druk de samengevoegde tekenreeks af

Hieronder vindt u de implementatie van de bovenstaande aanpak:

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>

Uitvoer

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

Tijdcomplexiteit: O(m+n)
Hulpruimte: O(1)

B. Functie gebruiken

Benadering:

  • De hoofdfunctie zal de functie concatenate_string() aanroepen om twee strings samen te voegen.
  • De functie haalt de lengte van string s op met behulp van strlen.
  • Nu zullen we het karakter van string s1 toevoegen aan s[i+j]. Deze stap wordt herhaald totdat er geen teken meer beschikbaar is in s1. We voegen tekens van string s1 toe aan s vanaf het einde van s.
  • Na de for-lus gaan we de string s aaneenschakelen.
  • Uiteindelijk zal de hoofdfunctie de string afdrukken die is samengevoegd.

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;> }>

Uitgang:

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

Tijdcomplexiteit: O(n+m) , waarbij n respectievelijk de grootte van string 1 en m de grootte van string 2 is.
Hulpruimte: O(1)

C. Recursie gebruiken

Benadering:

  • De functie concatenate_string() krijgt de strings s en s1.
  • als er geen elementen aanwezig zijn in s1, wijs dan s1 toe met een nulteken ( ).
  • Als er anders elementen aanwezig zijn, voegen we het element van string s1 toe aan het einde van string s en verhogen we de waarde van i met 1.
  • De functie concatenate_string zal zichzelf aanroepen door de gewijzigde strings s, s1 als argumenten door te geven. Deze functie zal zichzelf recursief aanroepen totdat er geen elementen meer beschikbaar zijn in 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;> }>

Uitgang:

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

Tijdcomplexiteit: O(n+m) , waarbij n respectievelijk de grootte van string 1 en m de grootte van string 2 is.
Hulpruimte: O(1)

2. De functie strcat() gebruiken

De functie strcat() in C voegt de kopie van de brontekenreeks toe aan de bestemming met een Null-teken aan het einde van de tekenreeks. Het komt onder het string.h-headerbestand in 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)>

Uitvoer

Final string is: techcodeview.com 

Tijdcomplexiteit: O(n+m) , waarbij n respectievelijk de grootte van string 1 en m de grootte van string 2 is.
Hulpruimte: O(1)