단어 처리
Coddy Java 여정의 논리와 흐름 섹션에 포함된 레슨 — 59개 중 51번째.
이제 구두점을 제거하고 소문자로 변환하여 각 단어를 처리하겠습니다. 이렇게 하면 대소문자나 주변 구두점에 상관없이 단어 수를 정확하게 셀 수 있습니다.
String word = "Hello!";
// 문장 부호를 제거하고 소문자로 변환합니다
word = word.replaceAll("[^a-zA-Z ]", "").toLowerCase();
System.out.println(word);
// 출력: hello챌린지
쉬움이전 프로그램을 수정하여 각 단어에서 구두점을 제거하고 소문자로 변환하여 정리하세요. 원본 단어와 처리된 단어를 모두 출력하세요.
예를 들어
입력:
Coddy
Expected Output:Original[0,0]: Coddy
Processed[0,0]: coddy직접 해보기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
String[] sentences = text.split("\\.");
String[][] textArray = new String[sentences.length][];
for (int i = 0; i < sentences.length; i++) {
textArray[i] = sentences[i].trim().split(" ");
}
for (int i = 0; i < textArray.length; i++) {
for (int j = 0; j < textArray[i].length; j++) {
if (!textArray[i][j].isEmpty()) {
System.out.println("Word[" + i + "," + j + "]: " + textArray[i][j]);
}
}
}
}
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.