文字列が回文であるかどうかを確認する方法

文字列が回文であるかどうかを確認する方法

元の弦とその逆の弦が同じである場合、その弦は回文であると言われます。この記事では、指定された文字列が回文であるかどうかを判断するためのアルゴリズムについて学習します。また、C ++、Python、C、JavaScriptなどの最も一般的なプログラミング言語でこのアルゴリズムを実装する方法についても学習します。





回文文字列の例

以下は、パリンドロームと非パリンドロームの文字列の例です。





特定の文字列が回文であるかどうかを判断するアルゴリズム

アルゴリズムは、何か有用なことをしたり、問題を解決したりするために、段階的に実行される一連の指示にすぎません。以下のアルゴリズムを使用して、文字列回文の問題を解決できます。





  1. 指定された文字列をパラメーターとして受け入れる関数を宣言します。
  2. ブール変数を作成し、trueに設定します。変数を 国旗
  3. 指定された文字列の長さを見つけます。長さを NS
  4. 指定された文字列を小文字に変換して、大文字と小文字を区別しない文字間の比較を行います。
  5. 低インデックス変数を次のように初期化します 低い 0に設定します。
  6. 高インデックス変数を次のように初期化します 高い n-1に設定します。
  7. 低い値が高い値よりも小さいときに、次の手順を実行します。
    • 低インデックスと高インデックスの文字を比較します。
    • 文字が一致しなかった場合は、フラグをfalseに設定して、ループを解除します。
    • lowの値を1インクリメントし、highの値を1デクリメントします。
  8. 関数の最後でフラグがtrueの場合、指定された文字列が回文であることを示します。
  9. 関数の最後でフラグがfalseの場合、指定された文字列が回文ではないことを示します。

特定の文字列が回文であるかどうかを確認するC ++プログラム

以下は、指定された文字列が回文であるかどうかを判別するためのC ++実装です。

Windows10のコマンドプロンプトのリスト
// Including libraries
#include
using namespace std;
// Function to check string palindrome
void checkPalindrome(string str)
{
// Flag to check if the given string is a palindrome
bool flag = true;

// Finding the length of the string
int n = str.length();

// Converting the string to lowercase
for(int i = 0; i {
str[i] = tolower(str[i]);
}

// Initializing low index variable
int low = 0;

// Initializing high index variable
int high = n-1;

// Running the loop until high is greater than low
while (high > low)
{
// If the characters are not same, set the flag to false
// and break from the loop
if(str[high] != str[low])
{
flag = false;
break;
}

// Increment the low index variable
low++;

// Decrement the high index variable
high--;
}

// Check if flag is true or false
if (flag)
{
cout << 'Yes, the given string is a palindrome' << endl;
}
else
{
cout << 'No, the given string is not a palindrome' << endl;
}

return;

}
int main()
{
// Test case: 1
string str1 = 'MUO';
checkPalindrome(str1);

// Test case: 2
string str2 = 'madam';
checkPalindrome(str2);

// Test case: 3
string str3 = 'MAKEUSEOF';
checkPalindrome(str3);

// Test case: 4
string str4 = 'racecar';
checkPalindrome(str4);

// Test case: 5
string str5 = 'mom';
checkPalindrome(str5);

return 0;
}

出力:



No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

特定の文字列が回文であるかどうかを確認するPythonプログラム

以下は、指定された文字列が回文であるかどうかを判断するためのPython実装です。

# Function to check string palindrome
def checkPalindrome(str):
# Flag to check if the given string is a palindrome
flag = True
# Finding the length of the string
n = len(str)
# Converting the string to lowercase
str = str.lower()
# Initializing low index variable
low = 0
# Initializing high index variable
high = n-1
# Running the loop until high is greater than low
while high > low:
# If the characters are not same, set the flag to false
# and break from the loop
if str[high] != str[low]:
flag = False
break
# Increment the low index variable
low = low + 1
# Decrement the high index variable
high = high - 1
# Check if flag is true or false
if flag:
print('Yes, the given string is a palindrome')
else:
print('No, the given string is not a palindrome')
# Test case: 1
str1 = 'MUO'
checkPalindrome(str1)
# Test case: 2
str2 = 'madam'
checkPalindrome(str2)
# Test case: 3
str3 = 'MAKEUSEOF'
checkPalindrome(str3)
# Test case: 4
str4 = 'racecar'
checkPalindrome(str4)
# Test case: 5
str5 = 'mom'
checkPalindrome(str5)

出力:





No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

与えられた文字列が回文であるかどうかをチェックするCプログラム

以下は、指定された文字列が回文であるかどうかを判別するためのC実装です。

// Including libraries
#include
#include
#include
#include
// Function to check string palindrome
void checkPalindrome(char str[])
{
// Flag to check if the given string is a palindrome
bool flag = true;
// Finding the length of the string
int n = strlen(str);
// Converting the string to lowercase
for(int i = 0; i {
str[i] = tolower(str[i]);
}
// Initializing low index variable
int low = 0;
// Initializing high index variable
int high = n-1;
// Running the loop until high is greater than low
while (high > low)
{
// If the characters are not same, set the flag to false
// and break from the loop
if(str[high] != str[low])
{
flag = false;
break;
}
// Increment the low index variable
low++;
// Decrement the high index variable
high--;
}
// Check if flag is true or false
if (flag)
{
printf('Yes, the given string is a palindrome ⁠n');
}
else
{
printf('No, the given string is not a palindrome ⁠n');
}
return;
}
int main()
{
// Test case: 1
char str1[] = 'MUO';
checkPalindrome(str1);
// Test case: 2
char str2[] = 'madam';
checkPalindrome(str2);
// Test case: 3
char str3[] = 'MAKEUSEOF';
checkPalindrome(str3);
// Test case: 4
char str4[] = 'racecar';
checkPalindrome(str4);
// Test case: 5
char str5[] = 'mom';
checkPalindrome(str5);
return 0;
}

出力:





テキストベースのゲームの作り方
No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

特定の文字列が回文であるかどうかを確認するJavaScriptプログラム

以下は、指定された文字列が回文であるかどうかを判別するためのJavaScript実装です。

// Function to check string palindrome
function checkPalindrome(str) {
// Flag to check if the given string is a palindrome
var flag = true;
// Finding the length of the string
var n = str.length;
// Converting the string to lowercase
str = str.toLowerCase();
// Initializing low index variable
var low = 0;
// Initializing high index variable
var high = n-1;
// Running the loop until high is greater than low
while (high > low) {
// If the characters are not same, set the flag to false
// and break from the loop
if(str[high] != str[low]) {
flag = false;
break;
}
// Increment the low index variable
low++;
// Decrement the high index variable
high--;
}
// Check if flag is true or false
if (flag) {
console.log('Yes, the given string is a palindrome');
} else {
console.log('No, the given string is not a palindrome');
}
}
// Test case: 1
var str1 = 'MUO';
checkPalindrome(str1);
// Test case: 2
var str2 = 'madam';
checkPalindrome(str2);
// Test case: 3
var str3 = 'MAKEUSEOF';
checkPalindrome(str3);
// Test case: 4
var str4 = 'racecar';
checkPalindrome(str4);
// Test case: 5
var str5 = 'mom';
checkPalindrome(str5);

出力:

No, the given string is not a palindrome
Yes, the given string is a palindrome
No, the given string is not a palindrome
Yes, the given string is a palindrome
Yes, the given string is a palindrome

プログラミングで文字列を処理する方法を学ぶ

文字列の操作はプログラミングの不可欠な部分です。 Python、JavaScript、C ++などのプログラミング言語で文字列を使用および操作する方法を知っている必要があります。

最初に使用する言語を探している場合は、Pythonが最適です。

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

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

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

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

YuvrajChandraのその他の作品

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

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

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