[COS PRO 2급 JAVA] 팰린드롬 판단하기

옹잉·2023년 10월 10일

COS PRO 2급 JAVA

목록 보기
10/13

[문제]

[막혔던 점]

TMI) 8번 문제인데 빠트려서 마지막에 풀었다.
한 줄 수정 문제여서 코드 읽고 틀린 부분을 고쳐줬다.

[풀이]

import java.util.*;

class Main {
 boolean solution(String sentence){        
        String str = "";
        for(int i = 0; i < sentence.length(); i++){
            char c = sentence.charAt(i);
            if(c != ' ' && c != '.') str += c;
        }
        int len = str.length();
        for(int i = 0; i < len / 2; i++){
            if(str.charAt(i) != str.charAt(len - 1 - i)) return false;
        }
        return true;
    }

<테스트 케이스>

// 아래는 테스트케이스 출력을 해보기 위한 main 함수입니다. solution 함수를 수정하세요.
    public static void main(String[] args) {
        Main sol = new Main();
        String sentence1 = "never odd or even.";
        boolean ret1 = sol.solution(sentence1);
       
        System.out.println("solution 메소드의 반환 값은 " + ret1 + " 입니다.");
       
       
        String sentence2 = "palindrome";
        boolean ret2 = sol.solution(sentence2);
       
        System.out.println("solution 메소드의 반환 값은 " + ret2+ " 입니다.");
    }
}
profile
틀리더라도 🌸🌈🌷예쁘게 지적해주세요💕❣️

0개의 댓글