본문 바로가기
algorithm solving/general

특정 문자 뒤집기

by 일상코더 2023. 2. 16.

특정 문자 뒤집기

설명

영어 알파벳과 특수문자로 구성된 문자열이 주어지면 영어 알파벳만 뒤집고,

특수문자는 자기 자리에 그대로 있는 문자열을 만들어 출력하는 프로그램을 작성하세요.

 

입력

첫 줄에 길이가 100을 넘지 않는 문자열이 주어집니다.

 

출력

첫 줄에 알파벳만 뒤집힌 문자열을 출력합니다.

예시 입력 1 

a#vbad#da

예시 출력 1

a#ddab#va

 

Prac6 클래스

public class Prac6 {

    public String solution(String str) {
        String answer = "";

        char[] x = str.toCharArray();
        int lt = 0, rt = x.length-1;

        while (lt < rt) {
            if (!Character.isAlphabetic(x[lt])) {
                ++lt;
            }
            else if (!Character.isAlphabetic(x[rt])) {
                --rt;
            }
            else {
                char tmp = x[lt];
                x[lt] = x[rt];
                x[rt] = tmp;
                ++lt;
                --rt;
            }
        }
        answer = String.valueOf(x);
        return answer;
    }
}

 

메인 메서드

public class Main {
    public static void main(String[] args) {
        Prac6 s = new Prac6();

        Scanner sc = new Scanner(System.in);
        String str = sc.next();

        System.out.println(s.solution(str));
    }
}

'algorithm solving > general' 카테고리의 다른 글

회문 문자열  (0) 2023.02.16
중복 문자 제거  (0) 2023.02.16
뒤집은 소수  (0) 2022.09.28
소수(에라토스테네스 체)  (0) 2022.09.28
2. 단어 뒤집기  (0) 2022.09.27

댓글