logo

כיצד לשרשר שתי מחרוזות ב-c++?

חלק זה ידון בחיבור של שתי מחרוזות או יותר בשפת התכנות C++. שרשרת המחרוזת פירושה קבוצת התווים המשלבת שתי מחרוזות נוספות כדי להחזיר מחרוזת בודדת משורשרת. בזמן שרשור המיתרים, המחרוזת השנייה מתווספת בסוף המחרוזת הראשונה כדי ליצור מחרוזת בודדת.

לדוגמה, יש לנו שתי מחרוזות, ' Java 'ו' Tpoint ', ואנחנו רוצים לשרשר כדי ליצור מחרוזת אחת בתור Java + Tpoint = JavaTpoint.

כיצד לשרשר שתי מחרוזות ב-c++

בואו נדון בדרכים השונות לשרשור את המחרוזת הנתונה בשפת התכנות C++.

  1. שרשרת שתי מיתרים באמצעות for loop
  2. שרשרת שתי מיתרים באמצעות לולאת while
  3. שרשרת שתי מחרוזות באמצעות האופרטור +
  4. שרשרת שתי מחרוזות באמצעות הפונקציה strcat()
  5. שרשרת שתי מחרוזות באמצעות הפונקציה append()
  6. שרשור שתי מחרוזות באמצעות ירושה
  7. שרשרת שתי מחרוזות באמצעות פונקציית friend ופונקציית strcat()

תוכנית לשרשור שתי מחרוזות באמצעות for loop

בואו נשקול דוגמה לשילוב שתי מחרוזות באמצעות for loop בתכנות C++.

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

תוכנית לשרשור שתי מיתרים באמצעות לולאת while

בואו נשקול דוגמה לשילוב שתי מחרוזות באמצעות לולאת while בתכנות C++.

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

תוכנית לשרשרת שתי מחרוזות באמצעות האופרטור + ב-C++

+ מפעיל: זהו אופרטור '+' אריתמטי שפשוט מוסיף שתי מחרוזות כדי להחזיר מחרוזת משורשרת חדשה.

הבה נשקול דוגמה לשילוב שתי מחרוזות באמצעות האופרטור + בתכנות C++.

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

תוכנית לשרשור שתי מחרוזות באמצעות שיטת strcat()

פונקציה strcat(): ה-strcat הוא פונקציה מובנית של מחלקת string, אשר מוסיפה שתי מחרוזות תווים כדי להחזיר מחרוזת משורשרת.

תחביר

שיעור מופשט של java
 strcat ( char *arr1, char *arr2) 

ישנם שני מערכי תווים בתחביר לעיל, arr1 ו-arr2, שעברו בתוך הפונקציה strcat() כדי להחזיר מחרוזת משורשרת.

הבה נבחן דוגמה לשילוב שתי מחרוזות באמצעות הפונקציה strcat() בתכנות C++.

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>

תוכנית לשרשור שתי מחרוזות באמצעות הפונקציה append

append() פונקציה: א לְצַרֵף() function היא פונקציית ספרייה מוגדרת מראש המשמשת להוספה או הוספה של מחרוזת שנייה בסוף המחרוזת הראשונה כדי להחזיר מחרוזת בודדת.

shehzad poonawala

תחביר

 str1.append(str2); 

בתחביר לעיל, str2 הוא מחרוזת שנייה שיש להעביר בפונקציה append() שמכניסה את המחרוזת str2 בסוף המחרוזת str1.

הבה נשקול דוגמה לשילוב שתי מחרוזות באמצעות הפונקציה append() בתכנות C++.

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

תְפוּקָה

ירושה ב-c++
 Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! 

תוכנית לשרשור שתי מחרוזות באמצעות הירושה של המחלקה

הבה נשקול דוגמה לשילוב שתי מחרוזות באמצעות ירושה בתכנות C++.

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

תוכנית לשרשרת שתי מחרוזות באמצעות הפונקציה friend ופונקציית strcat()

הבה נשקול דוגמה לשילוב שתי מחרוזות באמצעות פונקציית friend ופונקציית strcat() בתכנות C++.

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