- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountCharacter.java
More file actions
Latest commit
35 lines (29 loc) · 1.34 KB
/
Copy pathCountCharacter.java
File metadata and controls
35 lines (29 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
packageweek01.basic;
importjava.util.Scanner;
publicclassCountCharacter {
publicstaticvoidmain(String[] args) {
System.out.println("<문자 등장 횟수 세기>");
// 9단계(기초): 문자 등장 횟수 세기 (대소문자 무시)
// 문제: 사용자로부터 문자열과 한 글자를 입력받아,
// 문자열에서 해당 글자가 몇 번 등장하는지 출력하는 프로그램을 작성하세요.
// (예: 문자열 "Hello World", 문자 'l' -> 3)
// 힌트: String length(), charAt(), toLowerCase(), trim(), for문
// trim() : 불필요한 공백 제거
// toLowerCase() / toUpperCase() : 대소문자 변환
// charAt(index) : index 번지에 있는 문자를 반환 char
intcnt = 0;
Scannerscanner = newScanner(System.in);
System.out.println("문자열을 입력해주세요.> ");
Stringstr = scanner.nextLine();
System.out.println("한 글자를 입력해주세요.> ");
Stringch = scanner.nextLine();
StringnewStr = str.trim().toLowerCase();
for(inti = 0; i < newStr.length(); i++) {
if(newStr.charAt(i) == ch.trim().toLowerCase().charAt(0)) {
cnt++;
}
}
System.out.println(ch+"는 총 "+cnt+"번 등장합니다.");
scanner.close();
}
}