www.acmicpc.net/problem/10174

 

10174번: 팰린드롬

팰린드롬은 앞으로 읽으나 뒤로 읽으나 똑같은 단어나 숫자들을 말한다. 일반적으로 대소문자를 구분하지 않지만, 공백은 구분한다. 다음은 팰린드롬의 예시이다. Anna Harrah Arora Nat tan 9998999 123

www.acmicpc.net

언어 : cpp

방법 : 

받은 스트링 순차적으로 비교하면 된다~

단, 대소문자 구분없으므로 대문자로 치환하거나 소문자로 치환해서 비교할 것

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isPalindrome(string str)
{
	int n, q;
	n = str.length();
	q = n / 2;
	
	for(int i = 0 ; i <q ; i++)
	{ 
		if (toupper(str[n-1]) == toupper(str[i]))
		{
			n -= 1;
		}
		else
		{
			return false;
		}
	}
	return true;
}
int main()
{
	int cnt = 0;
	string str;
	cin >> cnt ;

	cin.ignore();
	
	for (int i = 0; i < cnt; i++)
	{
		getline(cin, str);
		if (true == isPalindrome(str))
		{
			cout << "Yes" << endl;
		}
		else
		{
			cout << "No" << endl;
		}
	}

	return 0;

}

'Programming > 알고리즘' 카테고리의 다른 글

[프로그래머스] 타겟 넘버  (0) 2021.03.22
[프로그래머스] 크레인 인형뽑기 게임  (0) 2021.03.16
[백준][2577] 숫자의 개수  (0) 2020.10.17
백준 3190 뱀  (0) 2019.10.11
정렬 알고리즘 - 수정  (0) 2017.11.17
복사했습니다!