ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] CodingTest 문제 3번 - ex1
    카테고리 없음 2022. 11. 8. 17:49

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    public class appleIndex {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("영어 단어를 입력해주세요.");
    String findword = sc.next(); // 찾고싶은 단어 만들기

    System.out.println("찾고싶은 단어를 입력해주세요.");
    String input = sc.next();

    if (findword.indexOf(input) == -1) {
    System.out.println("-1");
    } else {
    System.out.println("단어의 인덱스 : " + findindex(input, findword));
    }
    }

    public static List<Integer> findindex(String input, String s) {
    List<Integer> indexList = new ArrayList<Integer>();
    int index = s.indexOf(input); // s스트링 문자안에서 input이라는 단어를 찾습니다.
    // 찾은 값은 index라는 int값에 저장됨.

    while (index != -1) {
    indexList.add(index);
    index = s.indexOf(input, index + input.length());
    System.out.println("현재 index 값 :" + index);
    // s스트링문장 안에서 input을 (index+input의 길이 값[찾는 단어길이]) 인덱스번호부터 찾는다.
    }

    return indexList;

    }
    }

Designed by Tistory.