- 문제 해결 :
문장이 주어졌을 때, 각 단어들만을 뒤집어 출력하는 문제이다.
먼저, 각 단어들을 뒤집는 방법에 대해서 생각해 보았다.
맨 앞의 글자가 맨 뒤로, 그 다음 글자는 뒤에서 2번째 글자로 ... 이렇게 차례로
변환하는 과정이 필요했기 때문에 이 과정은 먼저 들어온 것이 나중에 나가는 LIFO,
stack과 연관이 있다고 생각했다.
따라서 각 단어들을 split으로 자른 후, 차례로 stack에 넣고 마지막 글자까지
모두 들어간다면 stack에서 차례로 꺼내 출력하도록 하였다.
public class Q9093 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Character> stack = new Stack<>();
int N = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i=0;i<N;i++){
String [] strs = br.readLine().split(" ");
for(int j=0;j<strs.length;j++){
String s = strs[j];
for(int k=0;k<s.length();k++){
stack.push(s.charAt(k));
}
while(!stack.isEmpty()){
sb.append(stack.pop());
}
sb.append(" ");
}
sb.append("\n");
}
System.out.println(sb);
}
}```