June is Combung
단어 바꾸기 본문
package week3;
import java.util.Scanner;
public class Day12_9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String text = "Life is too short.";
System.out.println(text);
System.out.print("변경하고 싶은 단어를 입력하세요 : ");
String word = scan.nextLine();
char[] arr = new char[text.length()];
for (int i = 0; i < text.length(); i++) {
arr[i] = text.charAt(i);
}
int size = word.length();
int check = -1;
int idx = 0;
for (int i = 0; i < arr.length - size + 1; i++) {
int count = 0;
for (int j = 0; j < size; j++) {
if (arr[i + j] == word.charAt(j)) {
count += 1;
}
}
if (count == size) {
idx = i;
check = 1;
}
}
if (check == 1) {
int firstIdx = idx;
int lastIdx = idx + size;
System.out.print("무엇으로 바꾸시겠습니까?");
String change = scan.nextLine();
int length = change.length();
char[] temp = arr;
String front = "";
for (int i = 0; i < firstIdx; i++) {
front += temp[i];
}
System.out.println(front);
String back = "";
for (int i = lastIdx; i < temp.length; i++) {
back += temp[i];
}
System.out.println(back);
String result = front + change + back;
System.out.println(result);
} else {
System.out.println("해당 단어는 존재하지 않습니다.");
}
}
}
'java > 문자열' 카테고리의 다른 글
문자열 숫자검사 & 단어 검색 (0) | 2021.03.23 |
---|---|
끝말잇기 (0) | 2021.03.23 |
문자열 기본문제 (0) | 2021.03.23 |
타입캐스팅(강제형변환) (0) | 2021.03.23 |
나이계산 (0) | 2021.03.23 |
Comments