C++의 문자열 연결

C++의 문자열 연결

문자열은 문자를 저장하는 데 사용되는 데이터 구조 유형입니다. C++에서 문자열 연결은 문자열과 관련하여 가장 많이 논의되는 주제 중 하나입니다. 사용자 정의 방법을 사용하여 문자열을 연결하는 방법에는 여러 가지가 있으며, 사전 정의된 방법을 사용하여 문자열을 연결하는 방법에는 두 가지가 있습니다. 이 모든 방법을 확인해 보겠습니다.

문자열연결-(1)-(1)

문자열 연결 방법

연결하는 방법에는 6가지가 있습니다. 아래에 언급된 바와 같이:



  1. add( ) 함수를 사용합니다.
  2. '+' 연산자를 사용합니다.
  3. strcat( ) 함수를 사용합니다.
  4. 루프에 C++를 사용합니다.
  5. 상속을 사용합니다.
  6. Friend 함수와 strcat() 함수를 사용합니다.

1.append() 함수 사용하기

add() 함수는 다음의 멤버 함수입니다. 표준::문자열 클래스 . 이 함수를 사용하면 아래 예제와 같이 두 개의 std::string 객체(C++ 스타일 문자열)를 연결할 수 있습니다.

통사론:

string& string::append (const string& str);  Here,   str:   String to be appended. 

다음은 Append() 함수를 사용하여 문자열 연결을 위한 C++ 프로그램입니다.

C++




// C++ Program for string> // concatenation using append> #include> 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 

2. '+' 연산자 사용

이는 두 문자열을 연결하는 가장 쉬운 방법입니다. 그만큼 + 운영자 문자열을 추가합니다 그리고 연결된 문자열을 반환합니다. 이 방법은 C++ 스타일 문자열(std::string 개체)에만 작동하고 C 스타일 문자열(문자 배열)에는 작동하지 않습니다.

통사론:

string new_string = init + add; 

다음은 '+' 연산자를 사용하여 문자열을 연결하는 C++ 프로그램입니다.

C++




// C++ Program for string> // concatenation using '+' operator> #include> 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 

3. strcat( ) 함수 사용하기

C++ strcat() 함수는 다음에 정의된 내장 함수입니다. 헤더 파일. 이 함수는 두 문자열을 연결합니다 그리고 추가하다 결과는 끈. 이 함수는 C 스타일 문자열(문자 배열)에만 작동하고 C++ 스타일 문자열(std::string 객체)에는 작동하지 않습니다.

통사론:

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

다음은 strcat() 함수를 사용하여 문자열 연결을 위한 C++ 프로그램입니다.

C++




// C++ Program for string> // concatenation using strcat> #include> #include> 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 

4. for 루프 사용

루프를 사용하는 것은 문자열 연결의 가장 기본적인 방법 중 하나입니다. 여기서는 전체 문자열을 순회한 다음 다른 문자열을 순회하면서 요소를 하나씩 추가합니다. 최종 결과는 두 문자열로 구성된 연결된 문자열이 됩니다.

다음은 for 루프를 사용하여 문자열 연결을 위한 C++ 프로그램입니다.

C++




// C++ Program for string> // concatenation using for loop> #include> 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] !=> ' '> ; i++)> > {> > output += init[i];> > }> > // Adding element inside output> > // fromt add> > for> (> int> i = 0; add[i] !=> ' '> ; i++)> > {> > output += add[i];> > }> > cout < < output < < endl;> > return> 0;> }>

산출

this is init added now 

5. 상속 사용

다음은 상속을 사용한 문자열 연결을 위한 C++ 프로그램입니다.

C++




// C++ program for string concatenation> // using inheritance> #include> #include> 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 

6. Friend 함수와 strcat() 함수 사용하기

다음은 friend 함수와 strcat() 함수를 사용하여 문자열 연결을 위한 C++ 프로그램입니다.

C++




// C++ program for string concatenation> // using friend function and strcat()> #include> #include> 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 


인기 기사

범주

재미있는 기사