logo

Zřetězení řetězců v C++

Řetězec je typ datové struktury používaný pro ukládání znaků. Zřetězení řetězců v C++ je jedním z nejdiskutovanějších témat souvisejících s řetězci. Existuje několik metod pro zřetězení řetězců pomocí uživatelsky definovaných metod a několik metod pro zřetězení řetězců pomocí předem definovaných metod. Podívejme se na všechny tyto metody.

StringConcatenation-(1)-(1)



Metody řetězení řetězců

Existuje 6 metod zřetězení Tětiva jak je uvedeno níže:

  1. Použití funkce append().
  2. Pomocí operátoru „+“.
  3. Použití funkce strcat().
  4. Použití C++ pro Loop.
  5. Použití dědičnosti.
  6. Použití funkce Friend a funkce strcat().

1. Použití funkce append().

Funkce append() je členská funkce std::string třída . Pomocí této funkce můžeme zřetězit dva objekty std::string (řetězce stylu C++), jak je znázorněno v příkladu níže.

Syntax:



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

Níže je uveden program C++ pro zřetězení řetězců pomocí funkce append():

C++






// C++ Program for string> // concatenation using append> #include> using> namespace> std;> // Driver code> int> main()> {> >string init(>'this is init'>);> >string add(>' added now'>);> >// Appending the string.> >init.append(add);> >cout << init << endl;> >return> 0;> }>

>

vypněte režim vývojáře

>

Výstup

this is init added now>

2. Pomocí operátoru „+“.

Toto je nejjednodušší způsob zřetězení dvou řetězců. The + operátor přidává struny a vrátí zřetězený řetězec. Tato metoda funguje pouze pro řetězce stylu C++ (objekty std::string) a nefunguje s řetězci stylu C (pole znaků).

Syntax:

string new_string = init + add;>

Níže je uveden program C++ pro zřetězení řetězců pomocí operátoru „+“:

C++




// C++ Program for string> // concatenation using '+' operator> #include> using> namespace> std;> // Driver code> int> main()> {> >string init(>'this is init'>);> >string add(>' added now'>);> >// Appending the string.> >init = init + add;> >cout << init << endl;> >return> 0;> }>

organizace a architektura počítačů

>

>

Výstup

this is init added now>

3. Použití funkce strcat().

Funkce strcat() C++ je vestavěná funkce definovaná v hlavičkový soubor. Tato funkce zřetězí dva řetězce teplo a přidat a výsledek je uložen v teplo tětiva. Tato funkce funguje pouze pro řetězce ve stylu C (pole znaků) a nefunguje pro řetězce ve stylu C++ (objekty std::string).

Syntax:

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

Níže je uveden program C++ pro zřetězení řetězců pomocí funkce strcat():

C++




// C++ Program for string> // concatenation using strcat> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >char> init[] =>'this is init'>;> >char> add[] =>' added now'>;> >// Concatenating the string.> >strcat>(init, add);> >cout << init << endl;> >return> 0;> }>

>

návratový typ v jazyce Java

>

Výstup

this is init added now>

4. Použití pro smyčku

Použití smyčky je jednou z nejzákladnějších metod zřetězení řetězců. Zde přidáváme prvky jeden po druhém při procházení celého řetězce a poté dalšího řetězce. Konečným výsledkem bude zřetězený řetězec vytvořený z obou řetězců.

Níže je uveden program C++ pro zřetězení řetězců pomocí smyčky for:

C++




// C++ Program for string> // concatenation using for loop> #include> using> namespace> std;> // Driver code> int> main()> {> >string init(>'this is init'>);> >string add(>' added now'>);> >string output;> >// Adding element inside output> >// from init> >for> (>int> i = 0; init[i] !=>' '>; i++)> >{> >output += init[i];> >}> >// Adding element inside output> >// fromt add> >for> (>int> i = 0; add[i] !=>' '>; i++)> >{> >output += add[i];> >}> >cout << output << endl;> >return> 0;> }>

>

>

Výstup

this is init added now>

5. Použití dědičnosti

Níže je uveden program C++ pro zřetězení řetězců pomocí dědičnosti:

C++




// C++ program for string concatenation> // using inheritance> #include> #include> using> namespace> std;> > // Base class> class> base> {> >protected>:> >virtual> string concatenate(string &str1,> >string &str2) = 0;> };> > // Derive class> class> derive:>protected> base {> >public>:> >string concatenate (string &str1,> >string &str2)> >{> >string temp;> >temp = str1 + str2;> >return> temp;> >}> };> > // Driver code> int> main()> {> >string init(>'this is init'>);> >string add(>' added now'>);> > >// Create string object> >derive obj;> > >// Print string> >cout << obj.concatenate (init, add);> > >return> 0;> }>

>

>

Výstup

this is init added now>

6. Použití funkce Friend a strcat().

Níže je uveden program C++ pro zřetězení řetězců pomocí funkce friend a strcat():

C++




příkaz java switch

// C++ program for string concatenation> // using friend function and strcat()> #include> #include> using> namespace> std;> // Base class> class> Base {> >public>:> >char> init[100] =>'this is init'>;> >char> add[100] =>' added now'>;> > >friend> void> myfun(Base b);> };> > void> myfun (Base b)> {> >// Pass parameter to concatenate> >strcat> (b.init, b.add);> > >cout << b.init;> }> // Driver code> int> main()> {> > >// Create object of base class> >Base b;> > >// pass b object to myfun() to print> >// the concatenated string> >myfun(b);> > >return> 0;> }>

>

>

Výstup

this is init added now>