문자열이 회문인지 확인하는 방법

문자열이 회문인지 확인하는 방법

원래 문자열과 그 반대가 같으면 문자열을 회문이라고 합니다. 이 기사에서는 주어진 문자열이 회문인지 아닌지를 결정하는 알고리즘에 대해 배울 것입니다. 또한 C++, Python, C 및 JavaScript와 같은 가장 널리 사용되는 프로그래밍 언어에서 이 알고리즘을 구현하는 방법을 배우게 됩니다.





회문 문자열의 예

다음은 회문 및 회문이 아닌 문자열의 몇 가지 예입니다.





주어진 문자열이 회문인지 여부를 결정하는 알고리즘

알고리즘은 유용한 작업을 수행하거나 문제를 해결하기 위해 단계별로 따라야 하는 일련의 지침일 뿐입니다. 아래 알고리즘을 사용하여 문자열 회문 문제를 해결할 수 있습니다.





  1. 주어진 문자열을 매개변수로 받아들이는 함수를 선언하십시오.
  2. 부울 변수를 만들고 true로 설정합니다. 변수를 깃발 .
  3. 주어진 문자열의 길이를 찾습니다. 길이를 하자 N .
  4. 지정된 문자열을 소문자로 변환하여 문자 간의 비교를 대소문자를 구분하지 않도록 합니다.
  5. 낮은 인덱스 변수를 다음과 같이 초기화합니다. 낮은 그리고 0으로 설정합니다.
  6. 높은 인덱스 변수를 다음과 같이 초기화합니다. 높은 n-1로 설정합니다.
  7. 낮음이 높음보다 낮을 때 다음을 수행합니다.
    • 낮은 인덱스와 높은 인덱스의 문자를 비교합니다.
    • 문자가 일치하지 않으면 플래그를 false로 설정하고 루프를 중단합니다.
    • low 값을 1씩 증가시키고 high 값을 1 감소시킵니다.
  8. 플래그가 함수의 끝에서 참이면 주어진 문자열이 회문임을 나타냅니다.
  9. 함수의 끝에서 플래그가 false이면 주어진 문자열이 회문(palindrome)이 아님을 나타냅니다.

주어진 문자열이 회문인지 여부를 확인하는 C++ 프로그램

다음은 주어진 문자열이 회문인지 여부를 결정하기 위한 C++ 구현입니다.

Netflix에서 프로필을 삭제하는 방법
// 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이 탁월한 선택입니다.

공유하다 공유하다 트위터 이메일 파이썬을 배우시나요? 문자열을 조작하는 방법은 다음과 같습니다.

Python에서 문자열을 사용하고 조작하는 것은 어려워 보일 수 있지만 믿을 수 없을 정도로 간단합니다.

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

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

유브라지 찬드라가 참여한 작품 더보기

뉴스레터 구독

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

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