-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversString.java
More file actions
54 lines (39 loc) · 1.32 KB
/
Copy pathReversString.java
File metadata and controls
54 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Algorithms;
import java.util.Scanner;
/**
* 주어진 String에 모든 단어를 거꾸로 하시오.
*
* ex)
* Input: "abc 123 apple"
* Output: "cba 321 elppa"
*/
public class ReversString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String inputText = in.nextLine();
// 스페이스단위로 잘라서 배열 만들기
String[] wordAry = inputText.split(" ");
System.out.println(convertToReverse(wordAry));
}
// 전달받은 배열을 돌면서 배열 길이 만큼 " " 공백 표시
public static String convertToReverse(String[] wordAry) {
StringBuilder strBuilder = new StringBuilder();
int lastCount = 0;
for(String word : wordAry) {
++ lastCount;
strBuilder.append(getReverseString(word));
// 마지막 공백은 추가하지 않음
if(lastCount < wordAry.length)
strBuilder.append(" ");
}
return strBuilder.toString();
}
// 문자열 뒤집기
public static String getReverseString(String text) {
StringBuilder strBuilder = new StringBuilder();
for(int i = text.length()-1; i >= 0; i --) {
strBuilder.append(text.charAt(i));
}
return strBuilder.toString();
}
}