본문 바로가기

ETC/자격증

[정보처리기사실기]두목넷 무료 강의 Part 10 프로그래밍 언어 활용 7

반응형

https://youtu.be/qnrLJwWF4nQ

 

Q>

문자열 String 이 보관하는 내용 중에서 문자 'a'가 포함되어 있는 개수를 출력하도록 구현하시오.

 

A>

public class MyClass {
    public static void main(String args[]) {
      String text = "Love is a variety of different feelings, states, and"
      + "attitudes that ranges from interpersonal affection to pleasure.";  // 문자열 상수
      int cnt = 0;
      for(int i=0; i< text.length();i++)
        if(text.charAt(i) == 'a')cnt++;
    
      System.out.println("a문자: "+ cnt);
    }
}

 

a문자: 10

반응형