【Java】文字列に含まれる特定の文字をカウントする(A - Something on It)
解いた問題
AtCoderの「A - Duplex Printing」
提出した回答
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String S = sc.next(); sc.close(); int counter = 0; for (char c: S.toCharArray()) { if (c == 'o') { counter += 1; } } int sum = 700 + 100 * counter; System.out.println(sum); } }
文字列を分解して、ループを回してカウントした。
Stringクラスにカウントする機能がないか探したが、見つけられなかった。
この問題は素直に解くのが良いのかもしれない。
ネットで、他の方は文字列に含まれる特定文字のカウントをどのようにしているか見てみた。
こちらのブログの方のように
を使用している例が多かった。他には同様の処理だがStreamを使ったり、外部のライブラリを使ったりしていた。
TODO
- 他のA問題を解く
おわり。。