Programmeerijana olete tõenäoliselt silmitsi olukorraga, mis nõuab stringi ümberpööramist. Stringi ümberpööramine on üks levinumaid olukordi, millega programmeerijad koodimist õppides silmitsi seisavad. Stringi saab ümber pöörata, kasutades sisseehitatud funktsioone või kirjutades vastupidise funktsiooni enda rakenduse.
Sellest artiklist saate teada erinevate meetodite kohta, kuidas stringi C ++, Python ja JavaScript keerata.
Erinevad meetodid stringi C ++ tagasipööramiseks
Stringi saab C ++ -is ümber pöörata, kasutades järgmisi meetodeid:
Stringi ümberpööramine C ++ -is, kasutades sisseehitatud tagasikäigu () funktsiooni
Allpool on programm C ++ stringide tagasipööramiseks sisseehitatud abil tagurpidi() funktsioon:
// 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äljund:
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öörake C ++ -st string ümber, vahetades tähemärke
Allpool on programm C ++, mis võimaldab stringi ümber pöörata, vahetades tähemärke:
// 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi ümberpööramine C ++ -is, kasutades konstruktoriga tagurpidi iteraatoreid
Allpool on C ++ programm stringi tagasipööramiseks, kasutades konstruktoriga tagurpidi iteraatoreid:
// 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äljund:
kuidas ühendada viimane fm spotifyga
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi ümberpööramine C ++ keeles, kasutades ajutist stringi
Allpool on programm C ++, mis muudab stringi ajutise stringi abil:
// 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Seotud: Kuidas leida vokaalist kaashäälikuid, numbreid ja erimärke
Erinevad meetodid stringi tagasipööramiseks Pythonis
Stringi saab Pythonis ümber pöörata, kasutades järgmisi meetodeid:
Stringi pööramine Pythonis, kasutades laiendatud viilude süntaksit
Allpool on Pythoni programm stringi ümberpööramiseks laiendatud viilude süntaksi abil:
# 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi pööramine Pythonis rekursiooni abil
Allpool on Pythoni programm stringi tagasipööramiseks rekursiooni abil:
Seotud: Mis on rekursioon ja kuidas seda kasutada?
# 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi pööramine Pythonis, kasutades sisseehitatud tagurpidi () meetodit
Allpool on Pythoni programm stringi tagasipööramiseks sisseehitatud abil tagurpidi () meetod:
# 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi pööramine Pythonis ajutise stringi abil
Allpool on Pythoni programm stringi ümberpööramiseks ajutise stringi abil:
# 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Erinevad meetodid stringi tagasipööramiseks JavaScriptis
Stringi saab JavaScriptis ümber pöörata, kasutades järgmisi meetodeid:
Seotud: Kuidas luua oma esimene Reacti rakendus JavaScripti abil
Stringi ümberpööramine JavaScriptis rekursiooni abil
Allpool on JavaScripti programm stringi tagasipööramiseks rekursiooni abil:
// 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi ümberpööramine JavaScriptis, kasutades sisseehitatud meetodeid
Allpool on JavaScripti programm stringi tagasipööramiseks sisseehitatud meetodite abil:
// 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Stringi ümberpööramine JavaScriptis, kasutades ajutist stringi
Allpool on JavaScripti programm stringi ümberpööramiseks ajutise stringi abil:
// 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äljund:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Õppige stringidega manipuleerimist
Stringiga seotud intervjuuprobleemide lahendamiseks peate teadma, kuidas stringiga manipuleerida. Stringiga saate manipuleerida mis tahes programmeerimiskeeles, näiteks C ++, Python, JavaScript, Java, C jne.
Python pakub stringi manipuleerimiseks kõige hõlpsamini mõistetavat süntaksi. Kui stringiga manipuleerimine tundub teile keeruline, andke Pythonile võimalus; see on petlikult lihtne.
Jaga Jaga Piiksuma E -post Pythoni õppimine? Siit saate teada, kuidas stringidega manipuleeridaStringide kasutamine ja manipuleerimine Pythonis võib tunduda keeruline, kuid see on petlikult lihtne.
Loe edasi Seotud teemad- Programmeerimine
- JavaScript
- Python
- Kodeerimise õpetused
Yuvraj on arvutiteaduse bakalaureuseõppe üliõpilane Indias Delhi ülikoolis. Ta on kirglik Full Stacki veebiarenduse vastu. Kui ta ei kirjuta, uurib ta erinevate tehnoloogiate sügavust.
kuidas snap score töötabVeel Yuvraj Chandrast
Telli meie uudiskiri
Liituge meie uudiskirjaga, et saada tehnilisi näpunäiteid, ülevaateid, tasuta e -raamatuid ja eksklusiivseid pakkumisi!
Tellimiseks klõpsake siin