본문
입력 문자열 역순 출력
프로그래밍/Java 2015. 12. 1. 12:36
# 입력 문자열 역순 출력
Source01) Reverse01.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package reverse; public class Reverse01 { private String string = ""; private String input_data = ""; public Reverse01(String input_data) { this.input_data = input_data; } public void getString() { System.out.println("원래 문자열 : " + input_data); for (int i = input_data.length() - 1; i > -1; i--) { string += input_data.charAt(i); } System.out.println("뒤집은 문자열01 : " + string); } } | cs |
Source02) Reverse02.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package reverse; public class Reverse02 { private String[] arr; private String string = ""; private String input_data = ""; Reverse02(String input_data) { this.input_data = input_data; } public void getString() { arr = input_data.split(" "); for (int i = arr.length - 1; i > -1; i--) { string += arr[i] + " "; } System.out.println("뒤집은 문자열02 : " + string); } } | cs |
Source03) Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | package reverse; public class Main { public static void main(String[] args) { String input_data = "I want to join your company"; Reverse01 reverse01 = new Reverse01(input_data); Reverse02 reverse02 = new Reverse02(input_data); reverse01.getString(); reverse02.getString(); } } | cs |
Result)
1 2 3 | 원래 문자열 : I want to join your company 뒤집은 문자열01 : ynapmoc ruoy nioj ot tnaw I 뒤집은 문자열02 : company your join to want I | cs |
댓글