String의 아래 메서드를 활용해서 주석처럼 출력하는 코드를 완성 하시오.
단, indexOf, lastIndexOf, substring 사용
/*
String의 아래 메서드를 활용해서 주석처럼 출력하는 코드를 완성 하시오.
단, indexOf, lastIndexOf,substring 사용
*/
class Homework {
public static void answer1(String str) {
// 0 1 2 34 5 6 7 891011121314
// String str = "태산이 높다하되 하늘 아래 뫼이로다";
// lastIndexof 사용
int idx = str.lastIndexOf(" "); // 14 , 뒤에서 빈칸은 14니까!
if(idx != -1) {
answer1(str.substring(0, idx)); // 높다하되 하늘 아래 뫼이로다.
}
System.out.println(str);
/*
태산이
태산이 높다하되
태산이 높다하되 하늘
태산이 높다하되 하늘 아래
태산이 높다하되 하늘 아래 뫼이로다.
*/
}
public static void answer2(String str) {
//indexOf
/*
뫼이로다.
아래 뫼이로다.
하늘 아래 뫼이로다.
높다하되 하늘 아래 뫼이로다.
태산이 높다하되 하늘 아래 뫼이로다.
*/
int idx = str.indexOf(" "); // 3
if(idx != -1) {
answer2(str.substring(idx + 1)); // 높다하되 하늘 아래 뫼이로다.
}
System.out.println(str);
}
public static void answer3(String str) {
//substring
/*
태산이
높다하되
하늘
아래
뫼이로다.
*/
int idx = str.indexOf(" ");
if(idx != -1){
System.out.println(str.substring(0, idx)); // 태산이 / 높다하되 / 하늘
answer3(str.substring(idx + 1));
} else {
System.out.println(str);
}
}
public static void answer4(String str) {
/*
뫼이로다.
아래
하늘
높다하되
태산이
*/
int idx = str.lastIndexOf(" "); // 태산이
if(idx != -1) {
System.out.println(str.substring(idx + 1));
answer4(str.substring(0, idx)); // 태산이 높다하되
} else {
System.out.println(str); // 태산이
}
}
public static void main(String[] args) {
String str = "태산이 높다하되 하늘 아래 뫼이로다.";
answer1(str);
System.out.println();
//System.out.println(str.substring(0,3));
//System.out.println(str.substring(0,8));
//System.out.println(str.substring(0,12));
//System.out.println(str.substring(0,15));
//System.out.println(str.substring(0,20));
answer2(str);
System.out.println();
//System.out.println(str.substring(15,20));
//System.out.println(str.substring(12,20));
//System.out.println(str.substring(9,20));
//System.out.println(str.substring(4,20));
//System.out.println(str.substring(0,20));
answer3(str);
System.out.println();
//String result1 = str.substring(0, str.indexOf("높"));
//System.out.println(result1);
//String result2 = str.substring(4, str.lastIndexOf("하"));
//System.out.println(result2);
answer4(str);
System.out.println();
}
}
강사님이 하신 코드는 여기 🔻
```java
class Homework_RecursiveCall {
public static void answer1(String str) {
int idx = str.lastIndexOf(" ");
if(idx != -1) {
answer1(str.substring(0, idx));
}
System.out.println(str);
}
public static void answer2(String str){
int idx = str.indexOf(" ");
if(idx != -1){
answer2(str.substring(idx+1));
}
System.out.println(str);
}
public static void answer3(String str){
int idx = str.indexOf(" ");
if(idx != -1){
System.out.println(str.substring(0, idx));
answer3(str.substring(idx+1, str.length()));
} else {
System.out.println(str);
}
}
public static void answer4(String str){
int idx = str.lastIndexOf(" ");
if(idx != -1) {
System.out.println(str.substring(idx+1, str.length()));
answer4(str.substring(0, idx));
} else {
System.out.println(str);
}
}
public static void main(String[] args) {
String str = "태산이 높다하되 하늘 아래 뫼이로다.";
answer1(str);
System.out.println();
answer2(str);
System.out.println();
answer3(str);
System.out.println();
answer4(str);
}
}
class Homework_Loop {
public static void answer1(String str) {
boolean flag = true;
int idx = 0;
while(flag) {
idx = str.indexOf(" ", idx);
if(idx == -1){
idx = str.length();
flag = false;
}
System.out.println(str.substring(0, idx));
idx++;
}
}
public static void answer2(String str) {
boolean flag = true;
int idx = str.length()-1;
while(flag) {
idx = str.lastIndexOf(" ", idx);
if(idx == -1) {
idx = 0;
flag = false;
} else {
idx++;
System.out.println(str.substring(idx));
idx -= 2;
}
}
public static void answer3(String src) {
int start = 0;
int end = 0;
boolean flag = true;
while(flag) {
end = src.indexOf(" ", start);
if(end == -1) {
flag = false;
end = src.length();
}
System.out.println(src.substring(start, end));
start = end + 1;
}
}
public static void answer4(String src) {
int start = src.length();
int end = src.length();
boolean flag = true;
while(flag) {
start = src.lastIndexOf(" ", start -1);
if(start == -1){
flag = false;
}
System.out.println(src.substring(start+1, end));
end = start;
start--;
}
}
public static void main(String[] args) {
String str = "태산이 높다하되 하늘 아래 뫼이로다.";
answer1(str);
System.out.println();
answer2(str);
System.out.println();
answer3(str);
System.out.println();
answer4(str);
System.out.println();
}
}