코딩테스트/자바 문제풀이

[CLASS 2: 구현] 백준 1259 팰린드롬수

승요나라 2024. 10. 7. 12:21

1259번: 팰린드롬수

https://www.acmicpc.net/problem/1259

 

# 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String input = br.readLine();
        while (!input.equals("0")) {
            String result = "yes";
            for (int i = 0; i < input.length(); i++) {
                if (!input.substring(i, i+1).equals(input.substring(input.length()-i-1, input.length()-i))) {
                    result = "no";
                    break;
                }
            }
            sb.append(result).append("\n");
            input = br.readLine();
        }
        System.out.println(sb);
        br.close();
    }
}
  • String을 다룰 때는 equals() 로 문자열의 내용이 동일한지 비교할 수 있다.
  • '==' 또는 '!=' 연산자는 각 문자열 객체의 주소를 비교하여 내용 비교와 전혀 상관이 없으므로 주의해야 한다.