C++, Python 및 JavaScript에서 문자열을 뒤집는 방법

C++, Python 및 JavaScript에서 문자열을 뒤집는 방법

프로그래머로서 문자열을 반대로 해야 하는 상황에 직면했을 가능성이 큽니다. 문자열 반전은 프로그래머가 코딩을 배우는 동안 직면하는 가장 일반적인 상황 중 하나입니다. 내장 함수를 사용하거나 반전 함수의 고유한 구현을 작성하여 문자열을 반전할 수 있습니다.





이 기사에서는 C++, Python 및 JavaScript에서 문자열을 반전시키는 다양한 방법에 대해 알아봅니다.





C++에서 문자열을 뒤집는 다양한 방법

다음 방법을 사용하여 C++에서 문자열을 반전할 수 있습니다.





내장 reverse() 함수를 사용하여 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++ 프로그램입니다.

// 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 프로그램입니다.

# 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에서 문자열 반전

다음은 재귀를 사용하여 문자열을 뒤집는 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

내장 reversed() 메서드를 사용하여 Python에서 문자열 반전

다음은 내장된 문자열을 사용하여 문자열을 뒤집는 Python 프로그램입니다. 반전() 방법:

# 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로 첫 번째 React 앱을 만드는 방법

재귀를 사용하여 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에서 문자열을 사용하고 조작하는 것은 어려워 보일 수 있지만 믿을 수 없을 정도로 간단합니다.

다음 읽기
관련 항목
  • 프로그램 작성
  • 자바스크립트
  • 파이썬
  • 코딩 튜토리얼
저자 소개 유브라지 찬드라(60편 게재)

Yuvraj는 인도 델리 대학교의 컴퓨터 공학 학부생입니다. 그는 풀 스택 웹 개발에 열정적입니다. 그는 글을 쓰지 않을 때 다양한 기술의 깊이를 탐구하고 있습니다.

구글 크롬이 메모리를 많이 사용하지 않게 하는 방법
유브라지 찬드라가 참여한 작품 더보기

뉴스레터 구독

기술 팁, 리뷰, 무료 전자책 및 독점 거래에 대한 뉴스레터에 가입하십시오!

구독하려면 여기를 클릭하세요.