Ako programátor ste sa pravdepodobne dostali do situácie, ktorá vyžaduje obrátenie reťazca. Prevrátenie reťazca je jednou z najčastejších situácií, s ktorými sa programátori pri učení kódovania stretávajú. Reťazec môžete obrátiť pomocou vstavaných funkcií alebo napísaním vlastnej implementácie reverznej funkcie.
V tomto článku sa dozviete o rôznych metódach na obrátenie reťazca v jazykoch C ++, Python a JavaScript.
Rôzne metódy na obrátenie reťazca v C ++
Reťazec v C ++ môžete obrátiť pomocou týchto metód:
Prevrátenie reťazca v C ++ pomocou vstavanej funkcie reverse ()
Nasleduje program C ++ na obrátenie reťazca pomocou vstavaného programu obrátiť () funkcia:
// C++ implementation to reverse a string
// using inbuilt function: reverse()
#include
using namespace std;
// Driver Code
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
reverse(str3.begin(), str3.end());
cout << 'Reversed string: ' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v C ++ zámenou znakov
Nasleduje program C ++ na obrátenie reťazca zámenou znakov:
// C++ implementation to reverse a string
// by swapping characters
#include
using namespace std;
// Own implementation of a function to reverse a string
void reverseString(string& str)
{
int size = str.size();
for(int i=0, j=size-1; i {
swap(str[i], str[j]);
}
}
// Driver Code
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
reverseString(str1);
reverseString(str2);
reverseString(str3);
cout << 'Reversed string: ' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v C ++ pomocou reverzných iterátorov s konštruktorom
Nasleduje program C ++ na obrátenie reťazca pomocou reverzných iterátorov s konštruktorom:
// C++ implementation to reverse a string
// using constructor
#include
using namespace std;
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
// Using reverse iterators to reverse a string
string reversedStr1 = string(str1.rbegin(), str1.rend());
string reversedStr2 = string(str2.rbegin(), str2.rend());
string reversedStr3 = string(str3.rbegin(), str3.rend());
cout << 'Reversed string: ' << endl;
cout << reversedStr1 << endl;
cout << reversedStr2 << endl;
cout << reversedStr3 << endl;
return 0;
}
Výkon:
vytvorte bootovacie USB z ISO Windows 7
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v C ++ pomocou dočasného reťazca
Nasleduje program C ++ na obrátenie reťazca pomocou dočasného reťazca:
// C++ implementation to reverse a string
// using a temporary string
#include
using namespace std;
// Function to reverse a string using a temporary string
string reverseString(string str)
{
int size = str.size();
string tempStr;
for(int i=size-1; i>=0; i--)
{
tempStr.push_back(str[i]);
}
return tempStr;
}
// Driver Code
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
cout << 'Reversed string: ' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Súvisiace články: Ako nájsť samohlásky, spoluhlásky, číslice a špeciálne znaky v reťazci
Rôzne metódy na obrátenie reťazca v Pythone
Reťazec v Pythone môžete obrátiť pomocou týchto metód:
Obráťte reťazec v Pythone pomocou syntaxe rozšírených segmentov
Nasleduje program Python na reverziu reťazca pomocou syntaxe rozšíreného segmentu:
# Python implementation to reverse a string
# using extended slice syntax
def reverseString(str):
return str[::-1]
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v Pythone pomocou rekurzie
Nasleduje program Python na reverziu reťazca pomocou rekurzie:
Súvisiace články: Čo je to rekurzia a ako ju používate?
# Python implementation to reverse a string
# using recursion
def reverseString(str):
if len(str) == 0:
return str
else:
return reverseString(str[1:]) + str[0]
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v Pythone pomocou vstavanej metódy obráteného ()
Nasleduje program Python na reverziu reťazca pomocou vstavaného programu obrátené () metóda:
# Python implementation to reverse a string
# using reversed method()
def reverseString(str):
str = ''.join(reversed(str))
return str
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v Pythone pomocou dočasného reťazca
Nasleduje program Python na reverziu reťazca pomocou dočasného reťazca:
# Python implementation to reverse a string
# using a temporary string
def reverseString(str):
tempStr = ''
for s in str:
tempStr = s + tempStr
return tempStr
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Rôzne metódy na obrátenie reťazca v JavaScripte
Reťazec v JavaScripte môžete obrátiť pomocou týchto metód:
Súvisiace články: Ako vytvoriť prvú reaktívnu aplikáciu pomocou JavaScriptu
Obráťte reťazec v JavaScripte pomocou rekurzie
Nasleduje program JavaScript na reverziu reťazca pomocou rekurzie:
// JavScript implementation to reverse a string
// using recursion
function reverseString(str) {
if (str === '') {
return '';
} else {
return reverseString(str.substr(1)) + str.charAt(0);
}
}
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
document.write('Input string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write('Reversed string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v jazyku JavaScript pomocou vstavaných metód
Nasleduje program JavaScript na reverziu reťazca pomocou vstavaných metód:
// JavaScript implementation to reverse a string
// using inbuilt methods
function reverseString(str) {
return str.split('').reverse().join('');
}
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
document.write('Input string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write('Reversed string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Obráťte reťazec v JavaScripte pomocou dočasného reťazca
Nasleduje program JavaScript na reverziu reťazca pomocou dočasného reťazca:
// JavScript implementation to reverse a string
// using a temporary string
function reverseString(str) {
var size = str.length;
tempStr = '';
for(let i=size-1; i>=0; i--)
{
tempStr += str[i];
}
return tempStr;
}
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
document.write('Input string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write('Reversed string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
Výkon:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Naučte sa manipulovať so strunami
Na riešenie problémov s pohovorom súvisiacim so strunou musíte vedieť, ako s reťazcom manipulovať. S reťazcom môžete manipulovať v akomkoľvek programovacom jazyku, ako je C ++, Python, JavaScript, Java, C atď.
Python poskytuje najľahšie zrozumiteľnú syntax na manipuláciu s reťazcom. Ak sa vám manipulácia s reťazcom zdá ťažká, vyskúšajte Python; je to klamlivo jednoduché.
zdieľam zdieľam Tweet E -mail Učíš sa Python? Tu je návod, ako manipulovať so strunamiPoužitie a manipulácia so reťazcami v Pythone sa môže zdať ťažké, ale je to klamlivo jednoduché.
Čítajte ďalej Súvisiace témy- Programovanie
- JavaScript
- Python
- Návody na kódovanie
Yuvraj je študentom informatiky na univerzite v Dillí v Indii. Je nadšený pre vývoj webových aplikácií Full Stack. Keď nepíše, skúma hĺbku rôznych technológií.
ako zastaviť hovory technickej podpory systému WindowsViac od Yuvraja Chandru
prihlásiť sa ku odberu noviniek
Pripojte sa k nášmu bulletinu a získajte technické tipy, recenzie, bezplatné elektronické knihy a exkluzívne ponuky!
Kliknutím sem sa prihlásite na odber