logo

Hvordan kan man sammenkæde to strenge i c++?

Dette afsnit vil diskutere sammenkædningen af ​​to eller flere strenge i programmeringssproget C++. Sammenkædningen af ​​strengen betyder den gruppe af tegn, der kombinerer yderligere to strenge for at returnere en sammenkædet enkelt streng. Mens strengene sammenkædes, tilføjes den anden streng for enden af ​​den første streng for at lave en enkelt streng.

For eksempel har vi to strenge, ' Java 'og' Tpoint ', og vi ønsker at sammenkæde for at lave en enkelt streng som Java + Tpoint = JavaTpoint.

Hvordan man sammenkæder to strenge i c++

Lad os diskutere de forskellige måder at sammenkæde den givne streng i C++ programmeringssproget.

java konverterer heltal til streng
  1. Sammenkæd to strenge med for loop
  2. Sammenkæd to strenge ved hjælp af while-løkke
  3. Sammenkæd to strenge ved hjælp af + operatoren
  4. Sammenkæd to strenge ved hjælp af strcat()-funktionen
  5. Sammenkæd to strenge ved hjælp af append()-funktionen
  6. Sammenkæd to strenge ved hjælp af arv
  7. Sammenkæd to strenge ved hjælp af ven-funktionen og strcat()-funktionen

Program til at sammenkæde to strenge ved hjælp af for loop

Lad os overveje et eksempel på at kombinere to strenge ved at bruge for loop i C++-programmeringen.

Program.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();>

Program til at sammenkæde to strenge ved hjælp af while-løkke

Lad os overveje et eksempel på at kombinere to strenge ved hjælp af en while-løkke i C++-programmering.

Program2.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;>

Program til at sammenkæde to strenge ved hjælp af + operatoren i C++

+ Operatør: Det er en aritmetisk '+'-operator, der blot tilføjer to strenge for at returnere en ny sammenkædet streng.

Lad os overveje et eksempel på at kombinere to strenge ved hjælp af + operatoren i C++ programmering.

Program3.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></\'>

Program til at sammenkæde to strenge ved hjælp af strcat()-metoden

strcat() funktion: Strcat er en indbygget funktion af strengklassen, som tilføjer to tegnstrenge for at returnere en sammenkædet streng.

Syntaks

den fulde form
 strcat ( char *arr1, char *arr2) 

Der er to tegnarrays i ovenstående syntaks, arr1 og arr2, som passerede inde i strcat()-funktionen for at returnere en sammenkædet streng.

Lad os overveje et eksempel på at kombinere to strenge ved hjælp af strcat()-funktionen i C++-programmeringen.

Program4.cpp

 #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>

Program til at sammenkæde to strenge ved hjælp af tilføjelsesfunktionen

tilføje() funktion: An Tilføj() funktion er en foruddefineret biblioteksfunktion, der bruges til at indsætte eller tilføje en anden streng i slutningen af ​​den første streng for at returnere en enkelt streng.

Syntaks

 str1.append(str2); 

I ovenstående syntaks er str2 en anden streng, der skal sendes i append()-funktionen, der indsætter str2-strengen i slutningen af ​​str1-strengen.

Lad os overveje et eksempel for at kombinere to strenge ved hjælp af append()-funktionen i C++-programmeringen.

Program5.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; } 

Produktion

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

Program til at sammenkæde to strenge ved hjælp af klassens arv

Lad os overveje et eksempel på at kombinere to strenge ved hjælp af arv i C++-programmeringen.

find blokerede numre på Android

Program6.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></\'>

Program til at sammenkæde to strenge ved hjælp af ven-funktionen og strcat()-funktionen

Lad os overveje et eksempel på at kombinere to strenge ved hjælp af ven-funktionen og strcat()-funktionen i C++-programmeringen.

Program7.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></\\'>