Dviejų stygų sujungimas C
Atsižvelgiant į dvi eilutes str1 ir str2, mūsų užduotis yra sujungti šias dvi eilutes. Yra keli būdai, kaip sujungti dvi eilutes C kalba:
- Nenaudojant strcat() funkcijos
- Naudojant standartinį metodą
- Naudojant funkciją
- Naudojant rekursiją
- Naudojant strcat() funkciją
1. Dviejų eilučių sujungimas nenaudojant strcat() funkcijos
A. Standartinio metodo naudojimas
Input: str1 = 'hello', str2 = 'world' Output: helloworld Input: str1 = 'Geeks', str2 = 'World' Output: GeeksWorld
Metodas: naudojant operatorių „+“.
C++
#include> #include> using> namespace> std;> int> main() {> > string str1 => 'Geeks'> ;> > string str2 => 'ForGeeks'> ;> > string result = str1 + str2;> > cout < < result < < endl;> > return> 0;> }> |
Išvestis
GeeksForGeeks
Metodas: naudojant pridėjimo funkciją.
C++
#include> using> namespace> std;> int> main() {> > string str1 => 'hello'> ;> > string str2 => 'world'> ;> > cout < <> 'The Resultant String Is :'> < cout < return 0; }> |
Išvestis
The Resultant String Is : helloworld
Sudėtingumo analizė:
Laiko sudėtingumas: O(1).
Pagalbinė erdvė: O(1).
Metodas:
- Kad dvi eilutės būtų sujungtos
- Paskelbkite naujas eilutes, kad išsaugotumėte sujungtą eilutę
- Įdėkite pirmąją eilutę į naują eilutę
- Įdėkite antrą eilutę į naują eilutę
- Atspausdinkite sujungtą eilutę
Toliau pateikiamas pirmiau minėto metodo įgyvendinimas:
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> |
Išvestis
First string: Geeks Second string: World Concatenated string: GeeksWorld
Laiko sudėtingumas: O(m+n)
Pagalbinė erdvė: O(1)
B. Funkcijos naudojimas
Metodas:
- pagrindinė funkcija iškvies concatenate_string() funkciją, kad sujungtų dvi eilutes.
- Funkcija eilės s ilgį gaus strlen pagalba.
- Dabar mes pridėsime eilutės s1 simbolį ties s[i+j]. Šis veiksmas bus kartojamas tol, kol s1 nebus jokio simbolio. Mes pridedame eilutės s1 simbolius prie s nuo s pabaigos.
- Po for ciklo mes sujungsime eilutę s.
- Pagaliau pagrindinė funkcija išspausdins eilutę, kuri yra sujungta.
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;> }> |
Išvestis:
Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'
Laiko sudėtingumas: O(n+m) , kur n yra 1 eilutės dydis ir m yra atitinkamai 2 eilutės dydis.
Pagalbinė erdvė: O(1)
C. Rekursijos naudojimas
Metodas:
- Funkcija concatenate_string() gaus eilutes s ir s1.
- jei s1 nėra elementų, priskirkite s1 su nuliu ( ) simboliu.
- kitu atveju, jei yra elementų, eilutės s pabaigoje pridėsime elementą s1 ir padidinsime i reikšmę 1.
- Funkcija concatenate_string išsikvies, kaip argumentus perduodama modifikuotas eilutes s, s1. Ši funkcija išsikvies rekursyviai, kol s1 nebus pasiekiamų elementų.
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;> }> |
Išvestis:
Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'
Laiko sudėtingumas: O(n+m) , kur n yra 1 eilutės dydis ir m yra atitinkamai 2 eilutės dydis.
Pagalbinė erdvė: O(1)
2. Strcat() funkcijos naudojimas
Funkcija strcat() programoje C prideda šaltinio eilutės kopiją prie paskirties vietos su simboliu Null eilutės pabaigoje. Jis patenka į string.h antraštės failą 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)> |
Išvestis
Final string is: techcodeview.com
Laiko sudėtingumas: O(n+m) , kur n yra 1 eilutės dydis ir m yra atitinkamai 2 eilutės dydis.
Pagalbinė erdvė: O(1)