วิธีย้อนกลับสตริงใน C ++, Python และ JavaScript

วิธีย้อนกลับสตริงใน C ++, Python และ JavaScript

ในฐานะโปรแกรมเมอร์ คุณอาจต้องเผชิญกับสถานการณ์ที่คุณต้องย้อนกลับสตริง การกลับสตริงเป็นหนึ่งในสถานการณ์ทั่วไปที่โปรแกรมเมอร์ต้องเผชิญขณะเรียนรู้การเขียนโค้ด คุณสามารถย้อนกลับสตริงได้โดยใช้ฟังก์ชันในตัวหรือโดยการเขียนการใช้งานฟังก์ชันย้อนกลับของคุณเอง





ในบทความนี้ คุณจะได้เรียนรู้เกี่ยวกับวิธีการต่างๆ ในการย้อนกลับสตริงใน C++, Python และ JavaScript





วิธีการต่าง ๆ ในการย้อนกลับสตริงใน C ++

คุณสามารถย้อนกลับสตริงใน C ++ โดยใช้วิธีการเหล่านี้:





ย้อนกลับสตริงใน C ++ โดยใช้ฟังก์ชันย้อนกลับ () ในตัว

ด้านล่างนี้เป็นโปรแกรม C++ เพื่อย้อนกลับสตริงโดยใช้ built-in ย้อนกลับ() การทำงาน:

// 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 ++ โดยใช้ Reverse Iterators ด้วย Constructor

ด้านล่างนี้คือโปรแกรม 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

คุณสามารถย้อนกลับสตริงใน Python โดยใช้วิธีการเหล่านี้:

ย้อนกลับสตริงใน Python โดยใช้ Extended Slice Syntax

ด้านล่างนี้คือโปรแกรม Python เพื่อย้อนกลับสตริงโดยใช้ไวยากรณ์สไลซ์แบบขยาย:

# 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 โดยใช้ Recursion

ด้านล่างนี้คือโปรแกรม Python เพื่อย้อนกลับสตริงโดยใช้การเรียกซ้ำ:

ที่เกี่ยวข้อง: การเรียกซ้ำคืออะไรและคุณใช้งานอย่างไร

# 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 โดยใช้วิธีย้อนกลับ () ในตัว

ด้านล่างนี้เป็นโปรแกรม Python เพื่อย้อนกลับสตริงโดยใช้ built-in ย้อนกลับ () กระบวนการ:

# 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 โดยใช้สตริงชั่วคราว

ด้านล่างนี้คือโปรแกรม Python เพื่อย้อนกลับสตริงโดยใช้สตริงชั่วคราว:

# 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

วิธีการต่าง ๆ ในการย้อนกลับสตริงใน JavaScript

คุณสามารถย้อนกลับสตริงใน JavaScript โดยใช้วิธีการเหล่านี้:

ที่เกี่ยวข้อง: วิธีสร้างแอปตอบโต้แรกของคุณด้วย JavaScript

ย้อนกลับสตริงใน JavaScript โดยใช้การเรียกซ้ำ

ด้านล่างนี้คือโปรแกรม JavaScript เพื่อย้อนกลับสตริงโดยใช้การเรียกซ้ำ:

// 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 โดยใช้วิธีการในตัว

ด้านล่างนี้คือโปรแกรม JavaScript เพื่อย้อนกลับสตริงโดยใช้วิธีการในตัว:

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

ย้อนกลับสตริงใน JavaScript โดยใช้สตริงชั่วคราว

ด้านล่างนี้คือโปรแกรม JavaScript เพื่อย้อนกลับสตริงโดยใช้สตริงชั่วคราว:

// 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++, Python, JavaScript, Java, C เป็นต้น

Python มีไวยากรณ์ที่เข้าใจง่ายที่สุดในการจัดการสตริง หากการจัดการสตริงดูเหมือนยากสำหรับคุณ ลองใช้ Python ; มันตรงไปตรงมาหลอกลวง

แบ่งปัน แบ่งปัน ทวีต อีเมล การเรียนรู้หลาม? นี่คือวิธีจัดการกับสตริง

การใช้และจัดการสตริงใน Python อาจดูยาก แต่ก็ตรงไปตรงมา

อ่านต่อไป
หัวข้อที่เกี่ยวข้อง
  • การเขียนโปรแกรม
  • JavaScript
  • Python
  • บทเรียนการเข้ารหัส
เกี่ยวกับผู้เขียน ยุวราช จันทรา(60 บทความที่ตีพิมพ์)

Yuvraj เป็นนักศึกษาระดับปริญญาตรีสาขาวิทยาการคอมพิวเตอร์ที่มหาวิทยาลัยเดลี ประเทศอินเดีย เขาหลงใหลเกี่ยวกับ Full Stack Web Development เมื่อไม่ได้เขียน เขากำลังสำรวจความลึกของเทคโนโลยีต่างๆ

วิธีแก้ไขเสียงบน windows 10
เพิ่มเติมจาก Yuvraj Chandra

สมัครรับจดหมายข่าวของเรา

เข้าร่วมจดหมายข่าวของเราสำหรับเคล็ดลับทางเทคนิค บทวิจารณ์ eBook ฟรี และดีลพิเศษ!

คลิกที่นี่เพื่อสมัครสมาชิก