Vzhľadom na dva reťazce str1 a str2 je našou úlohou tieto dva reťazce zreťaziť. Existuje niekoľko spôsobov, ako spojiť dva reťazce v jazyku C:
- Bez použitia funkcie strcat().
- Pomocou štandardnej metódy
- Používanie funkcie
- Pomocou rekurzie
- Použitie funkcie strcat().
1. Reťazenie dvoch reťazcov bez použitia funkcie strcat().
A. Použitie štandardnej metódy
Input: str1 = 'hello', str2 = 'world' Output: helloworld Input: str1 = 'Geeks', str2 = 'World' Output: GeeksWorld>
Prístup: Použitie operátora „+“.
C++
#include> #include> using> namespace> std;> int> main() {> >string str1 =>'Geeks'>;> >string str2 =>'ForGeeks'>;> >string result = str1 + str2;> >cout << result << endl;> >return> 0;> }> |
>
>Výkon
GeeksForGeeks>
Prístup: Použitie funkcie pripojenia.
C++
#include> using> namespace> std;> int> main() {> >string str1 =>'hello'>;> >string str2 =>'world'>;> >cout<<>'The Resultant String Is :'>< cout< return 0; }> |
>
>Výkon
The Resultant String Is : helloworld>
Analýza zložitosti:
Časová zložitosť:O(1).
Pomocný priestor: O(1).
Prístup:
- Nechajte dva reťazce zreťaziť
- Ak chcete uložiť zreťazený reťazec, deklarujte nové reťazce
- Vložte prvý reťazec do nového reťazca
- Vložte druhý reťazec do nového reťazca
- Vytlačte zreťazený reťazec
Nižšie je uvedená implementácia vyššie uvedeného prístupu:
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> |
>
>Výkon
First string: Geeks Second string: World Concatenated string: GeeksWorld>
Časová zložitosť: O(m+n)
Pomocný priestor: O(1)
B. Používanie funkcie
Prístup:
- hlavná funkcia zavolá funkciu concatenate_string() na spojenie dvoch reťazcov.
- Funkcia získa dĺžku reťazca s pomocou strlen.
- Teraz pripojíme znak reťazca s1 na s[i+j]. Tento krok sa bude opakovať, kým v s1 nebude dostupný žiadny znak. Znaky reťazca s1 pripájame k s od konca s.
- Po slučke for budeme spájať reťazec s.
- Nakoniec hlavná funkcia vypíše reťazec, ktorý je zreťazený.
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;> }> |
>
>
Výkon:
Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'>
Časová zložitosť: O(n+m) , kde n je veľkosť reťazca 1 a m je veľkosť reťazca 2.
Pomocný priestor: O(1)
C. Použitie rekurzie
Prístup:
- Funkcia concatenate_string() získa reťazce s a s1.
- ak v s1 nie sú prítomné žiadne prvky, priraďte s1 znak null ( ).
- inak, ak sú prítomné prvky, pridáme prvok reťazca s1 na koniec reťazca s a zvýšime hodnotu i o 1.
- Funkcia concatenate_string sa zavolá tak, že ako argumenty odovzdá upravené reťazce s, s1. Táto funkcia sa bude volať rekurzívne, kým v s1 nebudú dostupné žiadne prvky.
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;> }> |
>
>
Výkon:
Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'>
Časová zložitosť: O(n+m) , kde n je veľkosť reťazca 1 a m je veľkosť reťazca 2.
Pomocný priestor: O(1)
2. Použitie funkcie strcat().
Funkcia strcat() v C pripojí kópiu zdrojového reťazca k cieľu so znakom Null na konci reťazca. Nachádza sa v hlavičkovom súbore string.h v 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++
rekurzia v jave
#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)> |
>
>Výkon
Final string is: techcodeview.com>
Časová zložitosť: O(n+m) , kde n je veľkosť reťazca 1 a m je veľkosť reťazca 2.
Pomocný priestor: O(1)