Algorithm/기타

[백준 9093번] 단어 뒤집기 (Java 풀이)

agility 2020. 1. 25. 16:50

백준알고리즘 9093번 : 단어 뒤집기

 

문장 전체를 뒤집는 것이 아니고, 공백으로 구분되어진 각 단어의 순서는 그대로 유지하되

각 단어를 거꾸로 출력하는 것이다.

split method를 이용해서 문장에 대한 배열을 만든 다음, 각 단어를 거꾸로 출력하면 되겠다.

 

 

 

 

풀이 과정


1. 문장을 String 값으로 받은 뒤, 공백으로 문장을 split한 String 배열을 생성한다.
2. 배열을 for each문으로 돌리되, 각 단어의 길이를 재고, 가장 마지막 character부터 출력한다.

 

 

 

소스 ▽

더보기
import java.util.Scanner;

public class Main {// 9093번 단어 뒤집기
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int testcase = Integer.parseInt(sc.nextLine());

		for (int i = 0; i < testcase; i++) {
			String sentence = sc.nextLine();
			String[] prt = sentence.split(" ");

			for (String string : prt) {
				for (int j = string.length() - 1; j >= 0; j--) {
					System.out.print(string.charAt(j));
				}
				System.out.print(" ");
			}
			System.out.println();

		}
	}
}