logo

Hvordan sette sammen to strenger i c++?

Denne delen vil diskutere sammenkoblingen av to eller flere strenger i programmeringsspråket C++. Sammenkoblingen av strengen betyr gruppen av tegn som kombinerer ytterligere to strenger for å returnere en sammenkoblet enkeltstreng. Mens du setter sammen strengene, legges den andre strengen til på slutten av den første strengen for å lage en enkelt streng.

For eksempel har vi to strenger, ' Java ' og ' Tpoint ', og vi ønsker å sette sammen for å lage en enkelt streng som Java + Tpoint = JavaTpoint.

Hvordan sette sammen to strenger i c++

La oss diskutere de forskjellige måtene å sette sammen den gitte strengen i programmeringsspråket C++.

str til int
  1. Slå sammen to strenger med for loop
  2. Sammenknytt to strenger med while-løkke
  3. Slå sammen to strenger ved å bruke +-operatoren
  4. Slå sammen to strenger ved å bruke strcat()-funksjonen
  5. Slå sammen to strenger ved å bruke append()-funksjonen
  6. Sammenknytt to strenger ved å bruke arv
  7. Slå sammen to strenger ved å bruke venn-funksjonen og strcat()-funksjonen

Program for å sette sammen to strenger ved å bruke for loop

La oss vurdere et eksempel for å kombinere to strenger ved å bruke 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 for å sette sammen to strenger med while-løkke

La oss vurdere et eksempel for å kombinere to strenger ved å bruke 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 for å sette sammen to strenger ved å bruke +-operatoren i C++

+ Operatør: Det er en aritmetisk '+'-operator som ganske enkelt legger til to strenger for å returnere en ny sammenkoblet streng.

La oss vurdere et eksempel for å kombinere to strenger ved å bruke +-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 for å sette sammen to strenger ved å bruke strcat()-metoden

strcat() funksjon: Strcat er en innebygd funksjon av strengklassen, som legger til to tegnstrenger for å returnere en sammenkoblet streng.

Syntaks

som full form
 strcat ( char *arr1, char *arr2) 

Det er to tegnarrayer i syntaksen ovenfor, arr1 og arr2, som gikk inne i strcat()-funksjonen for å returnere en sammenkoblet streng.

La oss vurdere et eksempel for å kombinere to strenger ved å bruke strcat()-funksjonen 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 for å sette sammen to strenger ved å bruke append-funksjonen

append() funksjon: An legge til() funksjon er en forhåndsdefinert bibliotekfunksjon som brukes til å sette inn eller legge til en andre streng på slutten av den første strengen for å returnere en enkelt streng.

Syntaks

 str1.append(str2); 

I syntaksen ovenfor er str2 en andre streng som skal sendes i append()-funksjonen som setter inn str2-strengen på slutten av str1-strengen.

La oss vurdere et eksempel for å kombinere to strenger ved å bruke append()-funksjonen 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; } 

Produksjon

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

Program for å sette sammen to strenger ved å bruke arven til klassen

La oss vurdere et eksempel for å kombinere to strenger ved å bruke arv i C++-programmeringen.

hvorfor markørgrensesnitt i java

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 for å sette sammen to strenger ved å bruke venn-funksjonen og strcat()-funksjonen

La oss vurdere et eksempel for å kombinere to strenger ved å bruke venn-funksjonen og strcat()-funksjonen 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></\\'>