Kaip programuotojas, tikriausiai susidūrėte su situacija, dėl kurios reikia pakeisti eilutę. Eilutės pakeitimas yra viena iš dažniausiai pasitaikančių situacijų, su kuriomis susiduria programuotojai, mokydamiesi koduoti. Galite pakeisti eilutę naudodami integruotas funkcijas arba parašę savo atvirkštinės funkcijos įgyvendinimą.
Šiame straipsnyje sužinosite apie įvairius būdus, kaip pakeisti eilutę C ++, „Python“ ir „JavaScript“.
Skirtingi metodai, kaip pakeisti eilutę C ++
Galite pakeisti eilutę C ++ naudodami šiuos metodus:
Pakeiskite eilutę C ++ naudodami funkciją Integruota atvirkštinė () funkcija
Žemiau yra C ++ programa, skirta pakeisti eilutę naudojant įmontuotą atvirkščiai () funkcija:
// 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;
}
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite eilutę C ++ keisdami simbolius
Žemiau yra C ++ programa, skirta pakeisti eilutę keičiant simbolius:
// 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;
}
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite eilutę C ++ naudodami atvirkštinius iteratorius su konstruktoriumi
Žemiau yra C ++ programa, skirta pakeisti eilutę naudojant atvirkštinius iteratorius su konstruktoriumi:
// 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;
}
Išėjimas:
kaip paslėpti „Facebook“
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite eilutę C ++ naudodami laikiną eilutę
Žemiau yra C ++ programa, skirta pakeisti eilutę naudojant laikiną eilutę:
// 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;
}
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Susiję: Kaip eilėje rasti balsių, priebalsių, skaitmenų ir specialiųjų simbolių
Skirtingi būdai pakeisti eilutę „Python“
Galite pakeisti eilutę „Python“ naudodami šiuos metodus:
Pakeiskite eilutę „Python“ naudodami išplėstinę pjūvio sintaksę
Žemiau yra „Python“ programa, skirta pakeisti eilutę naudojant išplėstinę pjūvio sintaksę:
# 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)
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite eilutę „Python“ naudodami rekursiją
Žemiau yra „Python“ programa, skirta pakeisti eilutę naudojant rekursiją:
Susijęs: Kas yra rekursija ir kaip ją naudoti?
# 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)
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite eilutę „Python“ naudodami integruotą atvirkštinį () metodą
Žemiau yra „Python“ programa, skirta pakeisti eilutę naudojant įmontuotą atvirkščiai () metodas:
# 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)
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite eilutę „Python“ naudodami laikiną eilutę
Žemiau yra „Python“ programa, skirta pakeisti eilutę naudojant laikiną eilutę:
# 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)
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Skirtingi būdai pakeisti „JavaScript“ eilutę
Galite pakeisti eilutę „JavaScript“ naudodami šiuos metodus:
Susiję: Kaip sukurti savo pirmąją „React“ programą naudojant „JavaScript“
Pakeiskite „JavaScript“ eilutę naudodami rekursiją
Žemiau yra „JavaScript“ programa, skirta pakeisti eilutę naudojant rekursiją:
// 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 + '
');
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite „JavaScript“ eilutę naudodami integruotus metodus
Žemiau yra „JavaScript“ programa, skirta pakeisti eilutę naudojant integruotus metodus:
// 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 + '
');
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Pakeiskite „JavaScript“ eilutę naudodami laikiną eilutę
Žemiau yra „JavaScript“ programa, skirta pakeisti eilutę naudojant laikiną eilutę:
// 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 + '
');
Išėjimas:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Išmokite manipuliuoti stygomis
Norėdami išspręsti su stygomis susijusias interviu problemas, turite žinoti, kaip manipuliuoti eilute. Galite manipuliuoti eilute bet kuria programavimo kalba, pvz., C ++, Python, JavaScript, Java, C ir kt.
„Python“ suteikia lengviausiai suprantamą sintaksę manipuliuoti eilute. Jei jums atrodo sunku manipuliuoti stygomis, pabandykite „Python“; tai apgaulingai paprasta.
Dalintis Dalintis „Tweet“ Paštu Mokytis Python? Štai kaip manipuliuoti eilutėmisNaudoti ir manipuliuoti eilutėmis „Python“ gali atrodyti sunku, tačiau tai apgaulingai paprasta.
Skaityti toliau Susijusios temos- Programavimas
- „JavaScript“
- Python
- Kodavimo pamokos
Yuvraj yra kompiuterių mokslo bakalauro studentas Delyje, Indijoje. Jis aistringas „Full Stack“ žiniatinklio kūrimui. Kai jis nerašo, jis tyrinėja skirtingų technologijų gylį.
„Windows 10 100“ disko naudojimo sistemaDaugiau iš Yuvraj Chandra
Prenumeruokite mūsų naujienlaiškį
Prisijunkite prie mūsų naujienlaiškio, kad gautumėte techninių patarimų, apžvalgų, nemokamų el. Knygų ir išskirtinių pasiūlymų!
Norėdami užsiprenumeruoti, spustelėkite čia