Kā programmētājs jūs, iespējams, saskārāties ar situāciju, kuras dēļ jums ir jāmaina virkne. Virknes maiņa ir viena no visbiežāk sastopamajām situācijām, ar kurām programmētāji saskaras, mācoties kodēt. Jūs varat mainīt virkni, izmantojot iebūvētās funkcijas vai rakstot savu reversās funkcijas ieviešanu.
Šajā rakstā jūs uzzināsit par dažādām metodēm, kā mainīt virkni C ++, Python un JavaScript.
Dažādas metodes, lai mainītu virkni C ++
Jūs varat mainīt virkni C ++, izmantojot šādas metodes:
Apgrieziet virkni C ++, izmantojot iebūvēto reverso () funkciju
Zemāk ir C ++ programma, lai mainītu virkni, izmantojot iebūvēto otrādi () 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;
}
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni C ++, nomainot rakstzīmes
Zemāk ir programma C ++, lai mainītu virkni, samainot rakstzīmes:
// 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;
}
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni C ++, izmantojot apgrieztos atkārtotājus ar konstruktoru
Zemāk ir C ++ programma, lai mainītu virkni, izmantojot apgrieztos iteratorus ar konstruktoru:
// 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;
}
Izeja:
kā iegūt vairāk vietas MacBook Air
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni C ++, izmantojot pagaidu virkni
Zemāk ir C ++ programma, lai mainītu virkni, izmantojot pagaidu virkni:
// 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;
}
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Saistītie: Kā atrast patskaņus, līdzskaņus, ciparus un īpašas rakstzīmes virknē
Dažādas metodes, lai mainītu virkni Python
Jūs varat mainīt virkni Python, izmantojot šādas metodes:
Apgrieziet virkni Python, izmantojot paplašināto šķēles sintaksi
Zemāk ir programma Python, lai mainītu virkni, izmantojot paplašinātu šķēles sintaksi:
# 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)
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni Python, izmantojot rekursiju
Zemāk ir programma Python, lai mainītu virkni, izmantojot rekursiju:
Saistīts: Kas ir rekursija un kā to izmantot?
# 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)
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni Python, izmantojot iebūvēto apgriezto () metodi
Zemāk ir programma Python, lai mainītu virkni, izmantojot iebūvēto otrādi () metode:
# 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)
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni Python, izmantojot pagaidu virkni
Zemāk ir programma Python, lai mainītu virkni, izmantojot pagaidu virkni:
# 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)
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Dažādas metodes, lai mainītu virkni JavaScript
Jūs varat mainīt virkni JavaScript, izmantojot šādas metodes:
Saistīts: Kā izveidot savu pirmo React lietotni, izmantojot JavaScript
Apgrieziet virkni JavaScript, izmantojot rekursiju
Zemāk ir JavaScript programma, lai mainītu virkni, izmantojot rekursiju:
// 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 + '
');
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni JavaScript, izmantojot iebūvētās metodes
Zemāk ir JavaScript programma, lai mainītu virkni, izmantojot iebūvētās metodes:
// 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 + '
');
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Apgrieziet virkni JavaScript, izmantojot pagaidu virkni
Zemāk ir JavaScript programma, lai mainītu virkni, izmantojot pagaidu virkni:
// 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 + '
');
Izeja:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Uzziniet manipulācijas ar stīgām
Lai atrisinātu ar virkni saistītas intervijas problēmas, jums jāzina, kā manipulēt ar virkni. Jūs varat manipulēt ar virkni jebkurā programmēšanas valodā, piemēram, C ++, Python, JavaScript, Java, C utt.
Python nodrošina visvienkāršāk saprotamo sintaksi, lai manipulētu ar virkni. Ja manipulēšana ar virkni jums šķiet sarežģīta, dodiet iespēju Python; tas ir maldinoši vienkārši.
Kopīgot Kopīgot Čivināt E -pasts Mācīties Python? Lūk, kā manipulēt ar virknēmStīgu izmantošana un manipulēšana Python var šķist sarežģīta, taču maldinoši vienkārša.
Lasīt Tālāk Saistītās tēmas- Programmēšana
- JavaScript
- Python
- Kodēšanas apmācības
Yuvraj ir datorzinātņu bakalaura students Deli universitātē, Indijā. Viņš aizraujas ar Full Stack tīmekļa izstrādi. Kad viņš neraksta, viņš pēta dažādu tehnoloģiju dziļumu.
kāpēc mana ziņa netiek piegādātaVairāk no Yuvraj Chandra
Abonējiet mūsu biļetenu
Pievienojieties mūsu informatīvajam izdevumam, lai iegūtu tehniskus padomus, pārskatus, bezmaksas e -grāmatas un ekskluzīvus piedāvājumus!
Noklikšķiniet šeit, lai abonētu