Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lesson04.home_work.level1.loops;
- public class CharsCounter {
- public static void main(String[] args) {
- System.out.println(countChars("Hello, world!", 'o'));
- System.out.println(countChars("", '1'));
- }
- static int countChars(String s, char ch) {
- int count = 0;
- char[] charMassive = s.toCharArray();
- for (char element : charMassive) {
- count = element == ch ? count + 1 : count;
- }
- return count;
- }
- }
- //output
- // "Hello, world!", 'o' => 2
- // "", '1' => 0
Add Comment
Please, Sign In to add comment