C ++, పైథాన్ మరియు జావాస్క్రిప్ట్‌లో స్ట్రింగ్‌ను ఎలా రివర్స్ చేయాలి

C ++, పైథాన్ మరియు జావాస్క్రిప్ట్‌లో స్ట్రింగ్‌ను ఎలా రివర్స్ చేయాలి

ప్రోగ్రామర్‌గా, మీరు స్ట్రింగ్‌ను రివర్స్ చేయాల్సిన పరిస్థితిని మీరు ఎదుర్కొన్నారు. కోడ్ నేర్చుకునేటప్పుడు ప్రోగ్రామర్‌లు ఎదుర్కొనే అత్యంత సాధారణ పరిస్థితుల్లో స్ట్రింగ్‌ని రివర్స్ చేయడం ఒకటి. అంతర్నిర్మిత ఫంక్షన్లను ఉపయోగించడం ద్వారా లేదా రివర్స్ ఫంక్షన్ యొక్క మీ స్వంత అమలును వ్రాయడం ద్వారా మీరు స్ట్రింగ్‌ను రివర్స్ చేయవచ్చు.





ఈ ఆర్టికల్లో, C ++, పైథాన్ మరియు జావాస్క్రిప్ట్‌లో స్ట్రింగ్‌ను రివర్స్ చేయడానికి మీరు వివిధ పద్ధతుల గురించి నేర్చుకుంటారు.





C ++ లో స్ట్రింగ్‌ను రివర్స్ చేయడానికి వివిధ పద్ధతులు

మీరు ఈ పద్ధతులను ఉపయోగించి C ++ లో స్ట్రింగ్‌ను రివర్స్ చేయవచ్చు:





అంతర్నిర్మిత రివర్స్ () ఫంక్షన్ ఉపయోగించి C ++ లో స్ట్రింగ్‌ను రివర్స్ చేయండి

అంతర్నిర్మిత ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి C ++ ప్రోగ్రామ్ క్రింద ఉంది రివర్స్ () ఫంక్షన్:

// 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;
}

అవుట్‌పుట్:



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

అక్షరాలను మార్చుకోవడం ద్వారా C ++ లో స్ట్రింగ్‌ను రివర్స్ చేయండి

అక్షరాలను మార్చుకోవడం ద్వారా స్ట్రింగ్‌ను రివర్స్ చేయడానికి C ++ ప్రోగ్రామ్ క్రింద ఉంది:

// 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;
}

అవుట్‌పుట్:





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

కన్స్ట్రక్టర్‌తో రివర్స్ ఐటరేటర్‌లను ఉపయోగించి సి ++ లో స్ట్రింగ్‌ను రివర్స్ చేయండి

కన్స్ట్రక్టర్‌తో రివర్స్ ఇటరేటర్‌లను ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి C ++ ప్రోగ్రామ్ క్రింద ఉంది:

// 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;
}

అవుట్‌పుట్:





కంప్యూటర్‌లో ఫోన్ స్క్రీన్‌ను ఎలా ప్రదర్శించాలి
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS

తాత్కాలిక స్ట్రింగ్ ఉపయోగించి C ++ లో స్ట్రింగ్‌ను రివర్స్ చేయండి

తాత్కాలిక స్ట్రింగ్ ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి C ++ ప్రోగ్రామ్ క్రింద ఉంది:

// 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;
}

అవుట్‌పుట్:

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

సంబంధిత: ఒక స్ట్రింగ్‌లో అచ్చులు, హల్లులు, అంకెలు మరియు ప్రత్యేక అక్షరాలను ఎలా కనుగొనాలి

పైథాన్‌లో స్ట్రింగ్‌ను రివర్స్ చేయడానికి వివిధ పద్ధతులు

మీరు ఈ పద్ధతులను ఉపయోగించి పైథాన్‌లో స్ట్రింగ్‌ను రివర్స్ చేయవచ్చు:

విస్తరించిన స్లైస్ సింటాక్స్ ఉపయోగించి పైథాన్‌లో స్ట్రింగ్‌ను రివర్స్ చేయండి

విస్తరించిన స్లైస్ వాక్యనిర్మాణాన్ని ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి పైథాన్ ప్రోగ్రామ్ క్రింద ఉంది:

# 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)

అవుట్‌పుట్:

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

రికరేషన్ ఉపయోగించి పైథాన్‌లో స్ట్రింగ్‌ను రివర్స్ చేయండి

రికరేషన్ ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి పైథాన్ ప్రోగ్రామ్ క్రింద ఉంది:

సంబంధిత: పునరావృతం అంటే ఏమిటి మరియు మీరు దానిని ఎలా ఉపయోగిస్తున్నారు?

# 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)

అవుట్‌పుట్:

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

అంతర్నిర్మిత రివర్స్డ్ () పద్ధతిని ఉపయోగించి పైథాన్‌లో స్ట్రింగ్‌ను రివర్స్ చేయండి

అంతర్నిర్మిత ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి పైథాన్ ప్రోగ్రామ్ క్రింద ఉంది తిరగబడింది () పద్ధతి:

# 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)

అవుట్‌పుట్:

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

తాత్కాలిక స్ట్రింగ్ ఉపయోగించి పైథాన్‌లో స్ట్రింగ్‌ను రివర్స్ చేయండి

తాత్కాలిక స్ట్రింగ్ ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి పైథాన్ ప్రోగ్రామ్ క్రింద ఉంది:

# 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)

అవుట్‌పుట్:

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

జావాస్క్రిప్ట్‌లో స్ట్రింగ్‌ను రివర్స్ చేయడానికి వివిధ పద్ధతులు

మీరు ఈ పద్ధతులను ఉపయోగించి జావాస్క్రిప్ట్‌లోని స్ట్రింగ్‌ను రివర్స్ చేయవచ్చు:

సంబంధిత: జావాస్క్రిప్ట్‌తో మీ మొదటి రియాక్ట్ యాప్‌ను ఎలా క్రియేట్ చేయాలి

రికరేషన్ ఉపయోగించి జావాస్క్రిప్ట్‌లో స్ట్రింగ్‌ను రివర్స్ చేయండి

పునరావృతాన్ని ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి జావాస్క్రిప్ట్ ప్రోగ్రామ్ క్రింద ఉంది:

// 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 + '
');

అవుట్‌పుట్:

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

అంతర్నిర్మిత పద్ధతులను ఉపయోగించి జావాస్క్రిప్ట్‌లోని స్ట్రింగ్‌ను రివర్స్ చేయండి

అంతర్నిర్మిత పద్ధతులను ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి జావాస్క్రిప్ట్ ప్రోగ్రామ్ క్రింద ఉంది:

// 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 + '
');

అవుట్‌పుట్:

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

తాత్కాలిక స్ట్రింగ్ ఉపయోగించి జావాస్క్రిప్ట్‌లోని స్ట్రింగ్‌ను రివర్స్ చేయండి

తాత్కాలిక స్ట్రింగ్ ఉపయోగించి స్ట్రింగ్‌ను రివర్స్ చేయడానికి జావాస్క్రిప్ట్ ప్రోగ్రామ్ క్రింద ఉంది:

// 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 + '
');

అవుట్‌పుట్:

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

స్ట్రింగ్ మానిప్యులేషన్ నేర్చుకోండి

స్ట్రింగ్-సంబంధిత ఇంటర్వ్యూ సమస్యలను పరిష్కరించడానికి, స్ట్రింగ్‌ను ఎలా మానిప్యులేట్ చేయాలో మీరు తప్పక తెలుసుకోవాలి. మీరు C ++, పైథాన్, జావాస్క్రిప్ట్, జావా, C మొదలైన ప్రోగ్రామింగ్ లాంగ్వేజ్‌లో స్ట్రింగ్‌ని మార్చవచ్చు.

స్ట్రింగ్‌ని తారుమారు చేయడానికి పైథాన్ చాలా సులభంగా అర్థమయ్యే వాక్యనిర్మాణాన్ని అందిస్తుంది. స్ట్రింగ్‌ని తారుమారు చేయడం మీకు కష్టంగా అనిపిస్తే, పైథాన్‌కు వెళ్లండి; ఇది మోసపూరితంగా సూటిగా ఉంటుంది.

షేర్ చేయండి షేర్ చేయండి ట్వీట్ ఇమెయిల్ పైథాన్ నేర్చుకోవడం? తీగలను ఎలా మార్చాలో ఇక్కడ ఉంది

పైథాన్‌లో తీగలను ఉపయోగించడం మరియు తారుమారు చేయడం కష్టంగా అనిపించవచ్చు, కానీ ఇది మోసపూరితంగా సూటిగా ఉంటుంది.

తదుపరి చదవండి
సంబంధిత అంశాలు
  • ప్రోగ్రామింగ్
  • జావాస్క్రిప్ట్
  • పైథాన్
  • కోడింగ్ ట్యుటోరియల్స్
రచయిత గురుంచి యువరాజ్ చంద్ర(60 కథనాలు ప్రచురించబడ్డాయి)

యువరాజ్ భారతదేశంలోని ఢిల్లీ విశ్వవిద్యాలయంలో కంప్యూటర్ సైన్స్ అండర్ గ్రాడ్యుయేట్ విద్యార్థి. అతను పూర్తి స్టాక్ వెబ్ డెవలప్‌మెంట్ పట్ల మక్కువ కలిగి ఉన్నాడు. అతను వ్రాయనప్పుడు, అతను వివిధ సాంకేతికతల లోతును అన్వేషిస్తున్నాడు.

iphone se హోమ్ బటన్ పనిచేయడం లేదు
యువరాజ్ చంద్ర నుండి మరిన్ని

మా వార్తాలేఖకు సభ్యత్వాన్ని పొందండి

టెక్ చిట్కాలు, సమీక్షలు, ఉచిత ఈబుక్‌లు మరియు ప్రత్యేకమైన డీల్స్ కోసం మా వార్తాలేఖలో చేరండి!

సభ్యత్వం పొందడానికి ఇక్కడ క్లిక్ చేయండి