Jak obrátit řetězec v C ++, Pythonu a JavaScriptu

Jak obrátit řetězec v C ++, Pythonu a JavaScriptu

Jako programátor jste pravděpodobně čelili situaci, která vyžaduje, abyste obrátili řetězec. Obrácení řetězce je jednou z nejběžnějších situací, s nimiž se programátoři při učení kódování setkávají. Řetězec můžete obrátit pomocí předdefinovaných funkcí nebo napsáním vlastní implementace reverzní funkce.





V tomto článku se dozvíte o různých metodách pro obrácení řetězce v C ++, Pythonu a JavaScriptu.





Různé metody pro obrácení řetězce v C ++

Řetězec v C ++ můžete obrátit pomocí těchto metod:





Převrátit řetězec v C ++ pomocí vestavěné funkce reverse ()

Níže je uveden program C ++ pro obrácení řetězce pomocí vestavěného zvrátit() funkce:

// 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ýstup:



Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v C ++ prohozením znaků

Níže je uveden program C ++ pro obrácení řetězce výměnou znaků:

// 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ýstup:





Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v C ++ pomocí reverzních iterátorů pomocí konstruktoru

Níže je uveden program C ++ pro obrácení řetězce pomocí reverzních iterátorů s konstruktorem:

// 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ýstup:





jak odblokovat Adobe Flash Player
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v C ++ pomocí dočasného řetězce

Níže je uveden program C ++ pro obrácení řetězce pomocí dočasného řetězce:

// 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ýstup:

Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Příbuzný: Jak najít v řetězci samohlásky, souhlásky, číslice a speciální znaky

Různé metody pro obrácení řetězce v Pythonu

Řetězec v Pythonu můžete obrátit pomocí těchto metod:

Obraťte řetězec v Pythonu pomocí syntaxe Extended Slice

Níže je uveden program Python pro obrácení řetězce pomocí syntaxe rozšířeného řezu:

# 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ýstup:

Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v Pythonu pomocí rekurze

Níže je uveden program Python pro obrácení řetězce pomocí rekurze:

Související: Co je to rekurze a jak ji používáte?

# 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ýstup:

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átit řetězec v Pythonu pomocí metody Built-in reverseed ()

Níže je uveden program Python pro obrácení řetězce pomocí vestavěného obráceně () metoda:

# 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ýstup:

Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v Pythonu pomocí dočasného řetězce

Níže je uveden program Python pro obrácení řetězce pomocí dočasného řetězce:

# 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ýstup:

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ůzné metody pro obrácení řetězce v JavaScriptu

Řetězec v JavaScriptu můžete obrátit pomocí těchto metod:

Související: Jak vytvořit první aplikaci React pomocí JavaScriptu

Obraťte řetězec v JavaScriptu pomocí rekurze

Níže je uveden program JavaScript pro obrácení řetězce pomocí rekurze:

// 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ýstup:

Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v jazyce JavaScript pomocí integrovaných metod

Níže je uveden program JavaScript pro obrácení řetězce pomocí předdefinovaných metod:

// 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ýstup:

Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

Obraťte řetězec v JavaScriptu pomocí dočasného řetězce

Níže je uveden program JavaScript pro obrácení řetězce pomocí dočasného řetězce:

// 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ýstup:

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 se manipulaci se řetězci

Chcete-li řešit problémy s pohovorem souvisejícími se strunami, musíte vědět, jak s řetězcem manipulovat. S řetězcem můžete manipulovat v jakémkoli programovacím jazyce, jako je C ++, Python, JavaScript, Java, C atd.

Python poskytuje nejsrozumitelnější syntaxi pro manipulaci s řetězcem. Pokud se vám manipulace s řetězcem zdá obtížná, zkuste Python; je to zdánlivě jednoduché.

Podíl Podíl tweet E-mailem Učit se Python? Zde je návod, jak manipulovat s řetězci

Používání a manipulace s řetězci v Pythonu se může zdát obtížné, ale je klamně jednoduché.

Číst dále
Související témata
  • Programování
  • JavaScript
  • Krajta
  • Návody na kódování
O autorovi Yuvraj Chandra(60 článků zveřejněno)

Yuvraj je studentem informatiky na univerzitě v Dillí v Indii. Je nadšený pro webový vývoj Full Stack. Když nepíše, zkoumá hloubku různých technologií.

jak horké je příliš horké pro počítač
Více od Yuvraj Chandra

Přihlaste se k odběru našeho zpravodaje

Připojte se k našemu zpravodaji a získejte technické tipy, recenze, bezplatné elektronické knihy a exkluzivní nabídky!

Kliknutím sem se přihlásíte k odběru