Sammenkædning af to strenge i C

Givet to strenge str1 og str2, er vores opgave at sammenkæde disse to strenge. Der er flere måder at sammenkæde to strenge i C-sprog:

  • Uden at bruge strcat() funktion
    • Bruger standardmetoden
    • Bruger funktionen
    • Brug af rekursion
  • Brug strcat() funktion

1. Sammenkædning af to strenge uden at bruge strcat()-funktionen

A. Brug af standardmetode

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

Fremgangsmåde: Brug af operatoren '+'

C++




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

Produktion

GeeksForGeeks 

Fremgangsmåde: Brug af tilføjelsesfunktion.

C++




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

Produktion

The Resultant String Is : helloworld 

Kompleksitetsanalyse:

Tidskompleksitet:O(1).

Hjælperum: O(1).

Nærme sig:

  • Få de to strenge til at blive sammenkædet
  • Erklær nye strenge for at gemme den sammenkædede streng
  • Indsæt den første streng i den nye streng
  • Indsæt den anden streng i den nye streng
  • Udskriv den sammenkædede streng

Nedenfor er implementeringen af ​​ovenstående tilgang:

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>

Produktion

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

Tidskompleksitet: O(m+n)
Hjælpeplads: O(1)

B. Brug af funktion

Nærme sig:

  • hovedfunktionen kalder funktionen concatenate_string() for at sammenkæde to strenge.
  • Funktionen vil få længden af ​​streng s ved hjælp af strlen.
  • Nu vil vi tilføje tegnet af streng s1 ved s[i+j]. Dette trin vil blive gentaget, indtil ingen tegn er tilgængelig i s1. Vi tilføjer tegn af streng s1 til s fra slutningen af ​​s.
  • Efter for loop vil vi sammenkæde strengen s.
  • Til sidst vil hovedfunktionen udskrive strengen, som er sammenkædet.

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

Produktion:

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

Tidskompleksitet: O(n+m), hvor n er størrelsen af ​​streng 1 og m er størrelsen af ​​streng 2 hhv.
Hjælpeplads: O(1)

C. Brug af rekursion

Nærme sig:

  • Funktionen concatenate_string() vil få strengene s og s1.
  • hvis ingen elementer er til stede i s1, så tildel s1 med et nul-tegn ( ).
  • ellers, hvis elementer er til stede, tilføjer vi elementet af streng s1 i slutningen af ​​strengen s og vil øge værdien af ​​i med 1.
  • Funktionen concatenate_string vil kalde sig selv ved at sende de modificerede strenge s, s1 som argumenter. Denne funktion kalder sig selv rekursivt, indtil ingen elementer er tilgængelige i 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;> }>

Produktion:

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

Tidskompleksitet: O(n+m), hvor n er størrelsen af ​​streng 1 og m er størrelsen af ​​streng 2 hhv.
Hjælpeplads: O(1)

2. Brug strcat() funktion

strcat()-funktionen i C tilføjer kopien af ​​kildestrengen til destinationen med et Null-tegn i slutningen af ​​strengen. Det kommer under string.h header-fil i 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)>

Produktion

Final string is: techcodeview.com 

Tidskompleksitet: O(n+m), hvor n er størrelsen af ​​streng 1 og m er størrelsen af ​​streng 2 hhv.
Hjælpeplads: O(1)