찌니의 프로그래밍 삼매경

[알고리즘] 문자열 단어뒤집기 본문

알고리즘

[알고리즘] 문자열 단어뒤집기

zzI니☆ 2022. 10. 20. 11:05
728x90

👀 N개의 단어가 주어지면 각 단어를 뒤집어 출력하는 프로그램을 작성하세요.

 

 입력

첫 줄에 자연수 N(3<=N<=20)이 주어집니다.

두 번째 줄부터 N개의 단어가 각 줄에 하나씩 주어집니다. 단어는 영어 알파벳으로만 구성되어 있습니다.

3
good
Time
Big

 

 출력

N개의 단어를 입력된 순서대로 한 줄에 하나씩 뒤집어서 출력합니다.

doog
emiT
giB

import java.util.Scanner;

// 단어뒤집기
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        String words[] = new String[input];

        for(int i = 0; i < input; i++){
            words[i] = sc.next();
        }

        for(String x : words){
            String temp = new StringBuilder(x).reverse().toString();
            System.out.println(temp);
        }
    }
}
728x90
Comments