반응형

@notepad_jj2

츄르사려고 코딩하는 코집사입니다.


1. [SW expert Academy] SWEA 1213번 String 자바(Java)

 

2. 코드

import java.util.Scanner;
import java.util.Arrays;

public class Solution2 {
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        for(int tc =1; tc <=10; tc++)
        {
        	int t = sc.nextInt();
        	int result = 0;
        	int idx = -1;
        	
        	String a = sc.next();
        	String b = sc.next();
        	
        	int count = -1;
        	while((count = b.indexOf(a, count +1)) >= 0) result ++;
        	
        	System.out.printf("#%d %d",tc,result);
        	System.out.println();
        }
	}
}

- indexOf 사용하는데, indexOf는 아닐경우 -1을 리턴한다. 그래서, 0보다 크거나 같으면 result를 1씩 증가하여 출력


 

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기