SWEA - [d3] 1213 [S/W 문제해결 기본] 3일차 - String

Esther·2022년 11월 19일
0

SWEA

목록 보기
40/46

주어지는 영어 문장에서 특정한 문자열의 개수를 반환하는 프로그램을 작성하여라.

Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasicsofahealthydietandgoodnutrition.

위 문장에서 ti 를 검색하면, 답은 4이다.

[제약 사항]

총 10개의 테스트 케이스가 주어진다.

문장의 길이는 1000자를 넘어가지 않는다.

한 문장에서 검색하는 문자열의 길이는 최대 10을 넘지 않는다.

한 문장에서는 하나의 문자열만 검색한다.

[입력]

각 테스트 케이스의 첫 줄에는 테스트 케이스의 번호가 주어지고 그 다음 줄에는 찾을 문자열, 그 다음 줄에는 검색할 문장이 주어진다.

[출력]

#부호와 함께 테스트 케이스의 번호를 출력하고, 공백 문자 후 테스트 케이스의 답을 출력한다.

입력
1
ti
Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasics ...
2
ing
Thedoublehelixformsthestructuralbasisofsemi-conservativeDNAreplication.1,2Less ...
...

출력
#1 4
#2 2
...

package prc_d3;

import java.util.Scanner;

public class P1213 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = 10;// 총 10개의 테스트 케이스
		for (int tc = 1; tc <= T; tc++) {
			int t = sc.nextInt(); // 테스트케이스 번호
			int cnt = 0;

			String str = sc.next(); // 검색할 문자열
			String str2 = sc.next(); // 원본문자열

			str2 = str2.replace(str, "!"); // 검색할 문자열을 다른 문자로 바꾸어준다.

			for (int i = 0; i < str2.length(); i++) { // 원본문자열에서 바꾼문자를 찾아 count를 더해준다.
				if (String.valueOf(str2.charAt(i)).equals("!"))
					cnt++;

			}
			System.out.println("#" + t + cnt);
		}

	}

}

0개의 댓글