logo

Kā savienot divas virknes c++?

Šajā sadaļā tiks apspriesta divu vai vairāku virkņu savienošana C++ programmēšanas valodā. Virknes savienošana nozīmē rakstzīmju grupu, kas apvieno vēl divas virknes, lai atgrieztu sasaistītu vienu virkni. Savienojot virknes, otrā virkne tiek pievienota pirmās virknes beigās, lai izveidotu vienu virkni.

Piemēram, mums ir divas virknes, Java ' un ' Tpunkts ', un mēs vēlamies savienot, lai izveidotu vienu virkni kā Java + Tpoint = JavaTpoint.

a b c skaitļi
Kā savienot divas virknes programmā c++

Apspriedīsim dažādus veidus, kā sasaistīt doto virkni C++ programmēšanas valodā.

  1. Savienojiet divas virknes, izmantojot cilpu
  2. Savienojiet divas virknes, izmantojot cilpu while
  3. Savienojiet divas virknes, izmantojot operatoru +
  4. Savienojiet divas virknes, izmantojot funkciju strcat ().
  5. Savienojiet divas virknes, izmantojot funkciju append().
  6. Savienojiet divas virknes, izmantojot mantojumu
  7. Savienojiet divas virknes, izmantojot funkciju Draugs un funkciju strcat ().

Programma divu virkņu savienošanai, izmantojot cilpu

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot cilpu C++ programmēšanā.

Programma.cpp

 #include using namespace std; int main () { string str1, str2, result; // declare string variables int i; cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use for loop to enter the characters of the str1 into result string for ( i = 0; i <str1.size(); i++) { result="result" + str1[i]; add character of the str1 into } use for loop to enter characters str2 string ( i="0;" < str2.size(); str2[i]; cout << ' concatenation and is <<result; return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The Concatenation of the string Java and Tpoint is JavaTpoint </pre> <h3>Program to concatenate two strings using while loop</h3> <p>Let&apos;s consider an example to combine two strings using a while loop in C++ programming.</p> <p> <strong>Program2.cpp</strong> </p> <pre> #include using namespace std; int main () { // declare and initialize the string char str1[100] = &apos; We Love&apos;; char str2[100] = &apos; C++ Programming Language&apos;; int i, j; // declare variable cout &lt;&lt; &apos; The first string is: &apos; &lt;&lt; str1 &lt;&lt; endl; cout &lt;&lt; &apos; The second string is: &apos;&lt;&lt; str2 &lt;<endl; for (i="0;" str1[i] !="" ; i++); j="0;" initialize with 0; use while loop that insert the str2 characters in str1 (str2[j] ) check is not equal to null { assign character of i++; j++; } cout << ' concatenated string is: str1; return < pre> <p> <strong>Output</strong> </p> <pre> The first string is: We Love The second string is: C++ Programming Language The concatenated string is: We Love C++ Programming Language </pre> <h3>Program to concatenate two strings using the + operator in C++</h3> <p> <strong>+ Operator:</strong> It is an arithmetic &apos;+&apos;operator that simply adds two strings to return a new concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using the + operator in C++ programming.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2; // declare string variables cout &lt;&gt; str1; cout &lt;&gt; str2; // use &apos;+&apos; operator to concatenate the str1 and str2 string result = str1 + str2; cout &lt;&lt; &apos; The concatenated string &apos; &lt;&lt; str1 &lt;&lt; &apos; and &apos; &lt;&lt; str2 &lt;<' is: ' << result; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The concatenated string Java and Tpoint is: JavaTpoint </pre> <h3>Program to concatenate two strings using the strcat() method</h3> <p> <strong>strcat() function:</strong> The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.</p> <p> <strong>Syntax</strong> </p> <pre> strcat ( char *arr1, char *arr2) </pre> <p>There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using strcat() function in the C++ programming.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << ' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<' 
 the concatenated string is: ' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<' 100 enter the first string: '; cin.getline (st, 100); take a line of string with limit cout <<' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated ' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></'></pre></'></pre></str1></pre></'></pre></endl;></pre></str1.size();>

Programma divu virkņu savienošanai, izmantojot while cilpu

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot C++ programmēšanas cilpu while.

Programma2.cpp

 #include using namespace std; int main () { // declare and initialize the string char str1[100] = &apos; We Love&apos;; char str2[100] = &apos; C++ Programming Language&apos;; int i, j; // declare variable cout &lt;&lt; &apos; The first string is: &apos; &lt;&lt; str1 &lt;&lt; endl; cout &lt;&lt; &apos; The second string is: &apos;&lt;&lt; str2 &lt;<endl; for (i="0;" str1[i] !="" ; i++); j="0;" initialize with 0; use while loop that insert the str2 characters in str1 (str2[j] ) check is not equal to null { assign character of i++; j++; } cout << \' concatenated string is: str1; return < pre> <p> <strong>Output</strong> </p> <pre> The first string is: We Love The second string is: C++ Programming Language The concatenated string is: We Love C++ Programming Language </pre> <h3>Program to concatenate two strings using the + operator in C++</h3> <p> <strong>+ Operator:</strong> It is an arithmetic &apos;+&apos;operator that simply adds two strings to return a new concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using the + operator in C++ programming.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2; // declare string variables cout &lt;&gt; str1; cout &lt;&gt; str2; // use &apos;+&apos; operator to concatenate the str1 and str2 string result = str1 + str2; cout &lt;&lt; &apos; The concatenated string &apos; &lt;&lt; str1 &lt;&lt; &apos; and &apos; &lt;&lt; str2 &lt;<\' is: \' << result; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The concatenated string Java and Tpoint is: JavaTpoint </pre> <h3>Program to concatenate two strings using the strcat() method</h3> <p> <strong>strcat() function:</strong> The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.</p> <p> <strong>Syntax</strong> </p> <pre> strcat ( char *arr1, char *arr2) </pre> <p>There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using strcat() function in the C++ programming.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << \' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'></pre></str1></pre></\'></pre></endl;>

Programma divu virkņu savienošanai, izmantojot operatoru + C++

+ operators: Tas ir aritmētiskais '+'operators, kas vienkārši pievieno divas virknes, lai atgrieztu jaunu sasaistītu virkni.

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot operatoru + C++ programmēšanā.

Programma3.cpp

 #include using namespace std; int main () { string str1, str2; // declare string variables cout &lt;&gt; str1; cout &lt;&gt; str2; // use &apos;+&apos; operator to concatenate the str1 and str2 string result = str1 + str2; cout &lt;&lt; &apos; The concatenated string &apos; &lt;&lt; str1 &lt;&lt; &apos; and &apos; &lt;&lt; str2 &lt;<\' is: \' << result; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The concatenated string Java and Tpoint is: JavaTpoint </pre> <h3>Program to concatenate two strings using the strcat() method</h3> <p> <strong>strcat() function:</strong> The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.</p> <p> <strong>Syntax</strong> </p> <pre> strcat ( char *arr1, char *arr2) </pre> <p>There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using strcat() function in the C++ programming.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << \' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'></pre></str1></pre></\'>

Programma divu virkņu savienošanai, izmantojot strcat() metodi

strcat() funkcija: Strcat ir virkņu klases iebūvēta funkcija, kas pievieno divas rakstzīmju virknes, lai atgrieztu sasaistītu virkni.

Sintakse

 strcat ( char *arr1, char *arr2) 

Iepriekš minētajā sintaksē ir divi rakstzīmju masīvi, arr1 un arr2, kas tika nodoti funkcijā strcat (), lai atgrieztu sasaistītu virkni.

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot funkciju strcat() C++ programmēšanā.

Programma4.cpp

kas ir ziemas guļas režīms
 #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << \' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'></pre></str1>

Programma divu virkņu savienošanai, izmantojot pievienošanas funkciju

append() funkcija: An pievienot () funkcija ir iepriekš definēta bibliotēkas funkcija, ko izmanto, lai ievietotu vai pievienotu otru virkni pirmās virknes beigās, lai atgrieztu vienu virkni.

Sintakse

 str1.append(str2); 

Iepriekš minētajā sintaksē str2 ir otrā virkne, kas jānodod funkcijā append(), kas ievieto str2 virkni str1 virknes beigās.

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot funkciju append() C++ programmēšanā.

Programma5.cpp

 #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } 

Izvade

 Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! 

Programma divu virkņu savienošanai, izmantojot klases mantojumu

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot mantošanu C++ programmēšanā.

Programma6.cpp

 #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'>

Programma divu virkņu savienošanai, izmantojot draugs un strcat () funkciju

Apskatīsim piemēru, kā apvienot divas virknes, izmantojot funkciju Friend un strcat() C++ programmēšanā.

Programma7.cpp

 #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\\' 100 enter the first string: \\'; cin.getline (st, 100); take a line of string with limit cout <<\\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \\' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\\'>