C ++、Python、JavaScriptで文字列を逆にする方法

C ++、Python、JavaScriptで文字列を逆にする方法

プログラマーとして、文字列を逆にする必要がある状況に直面した可能性があります。文字列を逆にすることは、プログラマーがコーディングを学ぶときに直面する最も一般的な状況の1つです。組み込み関数を使用するか、逆関数の独自の実装を作成することにより、文字列を逆にすることができます。





この記事では、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プログラムです。

# 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プログラムです reverse() 方法:

# 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を試してみてください。それは一見簡単です。

共有 共有 つぶやき Eメール Pythonを学ぶ?文字列を操作する方法は次のとおりです

Pythonで文字列を使用および操作することは難しいように見えるかもしれませんが、それは一見簡単です。

次を読む
関連トピック
  • プログラミング
  • JavaScript
  • Python
  • コーディングチュートリアル
著者について ユブラジチャンドラ(60件の記事が公開されました)

Yuvrajは、インドのデリー大学のコンピュータサイエンス学部生です。彼はフルスタックWeb開発に情熱を注いでいます。執筆していないときは、さまざまなテクノロジーの深さを探っています。

このアクションを実行するには、システムからの許可が必要です
YuvrajChandraのその他の作品

ニュースレターを購読する

ニュースレターに参加して、技術的なヒント、レビュー、無料の電子書籍、限定セールを入手してください。

購読するにはここをクリックしてください