C++

C++ 문자열 연결(String Concatenation) 총정리

김구티2 2024. 7. 2. 19:35

1. 문자열 연결 도입

문자열은 문자를 저장하는 데 사용되는 일종의 데이터 구조다. 사용자 정의 방법, 사전에 정의된 방법을 사용하여 문자열을 연결하는 방법이 몇 가지 존재한다. 그 모든 것을 확인해 보도록 한다. 그리고 특수하게, C 스타일에서는 되지만 C++ 스타일에서는 되지 않는 것들도 전부 알아보도록 한다.

문자열을 연결하는 방법은 총 6가지이다.

append( ) 함수 사용

② + 연산자 사용

strcat( ) 함수 사용

④ for 루프 사용

⑤ 상속 사용

⑥ Friend 함수와 strcat( ) 함수 사용

2. append( ) 함수 사용하여 문자열 연결

append() 함수는 std::string 클래스의 멤버 함수다. 이 함수를 사용하면 아래 예시와 같이 두 개의 std::string 개체를 연결할 수 있다.

 

구문:

string& string::append (const string& str);

Here,
str: String to be appended.

 

예시:

// C++ Program for string
// concatenation using append
#include <iostream>
using namespace std;

// Driver code
int main()
{
     string init("this is init");
     string add(" added now");

     // Appending the string.
     init.append(add);

     cout << init << endl;
     return 0;
}

 

출력:

this is init added now

3. + 연산자 사용하여 문자열 연결

이것이 두 문자열을 연결하는 가장 쉬운 방법이다. + 연산자는 문자열을 추가하고 연결된 문자열을 반환한다. 이 방법은 C++ 스타일 문자열(std::string 개체)에만 작동하고, C 스타일 문자열(문자 배열)에는 작동하지 않는다.

 

구문:

string new_string = init + add;

 

예시:

// C++ Program for string
// concatenation using '+' operator
#include <iostream>
using namespace std;

// Driver code
int main()
{
     string init("this is init");
     string add(" added now");

     // Appending the string.
     init = init + add;

     cout << init << endl;
     return 0;
}

 

출력:

this is init added now

4. strcat( ) 함수 사용하여 문자열 연결

C++ strcat() 함수는 <string.h> 헤더 파일에 정의된 내장 함수다. 이 함수는 init와 add 두 문자열을 연결하고 결과는 init 문자열에 저장된다. 이 함수는 C 스타일 문자열(문자 배열)에만 작동하고, C++ 스타일 문자열(std::string 개체)에는 작동하지 않는다.

 

구문:

char * strcat(char * init, const char * add);

 

예시:

// C++ Program for string
// concatenation using strcat
#include <iostream>
#include <string.h>
using namespace std;

// Driver code
int main()
{
     char init[] = "this is init";
     char add[] = " added now";

     // Concatenating the string.
     strcat(init, add);

     cout << init << endl;

     return 0;
}

 

출력:

this is init added now

5. for 루프 사용하여 문자열 연결

루프를 이용하는 것은 가장 기본적인 문자열 연결 방법 중 하나다. 여기서는 전체 문자열을 가로지르며 원소들을 하나씩 추가한 다음, 다른 문자열을 추가한다. 최종 결과는 두 문자열로 이루어진 연결된 문자열이 될 것이다.

 

예시:

// C++ Program for string
// concatenation using for loop
#include <iostream>
using namespace std;

// Driver code
int main()
{
     string init("this is init");
     string add(" added now");

     string output;

     // Adding element inside output
     // from init
     for (int i = 0; init[i] != '\0'; i++) 
     {
          output += init[i];
     }

     // Adding element inside output
     // fromt add
     for (int i = 0; add[i] != '\0'; i++) 
     {
          output += add[i];
     }

     cout << output << endl;
     return 0;
}

 

출력:

this is init added now

6. 상속 사용하여 문자열 연결

예시:

// C++ program for string concatenation 
// using inheritance
#include <iostream> 
#include <string> 
using namespace std; 

// Base class 
class base 

     protected
          virtual string concatenate(string &str1, 
                                                     string &str2) = 0; 
}; 

// Derive class
class derive: protected base { 
     public
          string concatenate (string &str1, 
                                          string &str2) 
         
               string temp; 
               temp = str1 + str2; 
               return temp; 
         
}; 

// Driver code
int main() 

     string init("this is init");
     string add(" added now"); 

     // Create string object 
     derive obj; 

     // Print string 
     cout << obj.concatenate (init, add); 

     return 0; 
}

 

출력:

this is init added now

7.  Friend 함수와 strcat( ) 함수 사용하여 문자열 연결

예시:

// C++ program for string concatenation
// using friend function and strcat() 
#include <iostream> 
#include <string.h> 
using namespace std; 

// Base class 
class Base { 
     public
          char init[100] = "this is init";
          char add[100] = " added now"

          friend void myfun(Base b);  
}; 

void myfun (Base b) 

     // Pass parameter to concatenate 
     strcat (b.init, b.add); 

     cout << b.init; 


// Driver code
int main() 


     // Create object of base class 
     Base b; 

     // pass b object to myfun() to print 
     // the concatenated string 
     myfun(b); 

     return 0; 
}

 

출력:

this is init added now

728x90