두 개의 문자열 str1과 str2가 주어지면 우리의 임무는 이 두 문자열을 연결하는 것입니다. C 언어에서 두 문자열을 연결하는 방법에는 여러 가지가 있습니다.
- strcat() 함수를 사용하지 않고
- 표준 방법 사용
- 기능 사용
- 재귀 사용
- strcat() 함수 사용
1. strcat() 함수를 사용하지 않고 두 문자열 연결
A. 표준방법 사용
Input: str1 = 'hello', str2 = 'world' Output: helloworld Input: str1 = 'Geeks', str2 = 'World' Output: GeeksWorld>
접근법: '+' 연산자 사용
C++
#include> #include> using> namespace> std;> int> main() {> >string str1 =>'Geeks'>;> >string str2 =>'ForGeeks'>;> >string result = str1 + str2;> >cout << result << endl;> >return> 0;> }> |
>
>산출
GeeksForGeeks>
접근법: 추가 기능을 사용합니다.
C++
#include> using> namespace> std;> int> main() {> >string str1 =>'hello'>;> >string str2 =>'world'>;> >cout<<>'The Resultant String Is :'>< cout< return 0; }> |
>
>산출
The Resultant String Is : helloworld>
복잡성 분석:
시간 복잡도: O(1).
보조 공간: O(1).
접근하다:
- 연결할 두 문자열을 가져옵니다.
- 연결된 문자열을 저장하기 위해 새 문자열을 선언합니다.
- 새 문자열에 첫 번째 문자열을 삽입합니다.
- 새 문자열에 두 번째 문자열을 삽입합니다.
- 연결된 문자열을 인쇄합니다.
다음은 위의 접근 방식을 구현한 것입니다.
씨
// 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> |
>
>산출
First string: Geeks Second string: World Concatenated string: GeeksWorld>
시간 복잡도: O(m+n)
보조 공간 : 오(1)
B. 기능 사용
접근하다:
- main 함수는 concatenate_string() 함수를 호출하여 두 문자열을 연결합니다.
- 이 함수는 strlen의 도움으로 문자열 s의 길이를 가져옵니다.
- 이제 문자열 s1의 문자를 s[i+j]에 추가하겠습니다. 이 단계는 s1에서 사용할 수 있는 문자가 없을 때까지 반복됩니다. 문자열 s1의 문자를 s의 끝에서부터 s에 추가합니다.
- for 루프 후에 문자열 s를 연결합니다.
- 마지막으로 주 함수는 연결된 문자열을 인쇄합니다.
씨
// 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;> }> |
>
>
산출:
Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'>
시간 복잡도: O(n+m) , 여기서 n은 각각 문자열 1의 크기이고 m은 문자열 2의 크기입니다.
보조 공간: 오(1)
C. 재귀 사용
접근하다:
- concatenate_string() 함수는 문자열 s와 s1을 가져옵니다.
- s1에 요소가 없으면 s1에 null( ) 문자를 할당합니다.
- 그렇지 않으면 요소가 존재하면 문자열 s의 끝에 문자열 s1의 요소를 추가하고 i의 값을 1만큼 증가시킵니다.
- concatenate_string 함수는 수정된 문자열 s, s1을 인수로 전달하여 자신을 호출합니다. 이 함수는 s1에서 사용할 수 있는 요소가 없을 때까지 자신을 재귀적으로 호출합니다.
씨
// 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;> }> |
>
>
산출:
Enter the first string: Geeks Enter the second string: forGeeks Concatenated String is: 'techcodeview.com'>
시간 복잡도: O(n+m) , 여기서 n은 각각 문자열 1의 크기이고 m은 문자열 2의 크기입니다.
보조 공간: 오(1)
2. strcat() 함수 사용
C의 strcat() 함수는 문자열 끝에 Null 문자를 사용하여 소스 문자열의 복사본을 대상에 추가합니다. C의 string.h 헤더 파일 아래에 있습니다.
씨
// 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++
Java 메소드의 문자열
#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)> |
>
>산출
Final string is: techcodeview.com>
시간 복잡도: O(n+m) , 여기서 n은 각각 문자열 1의 크기이고 m은 문자열 2의 크기입니다.
보조 공간: 오(1)