Guest User

Untitled

a guest
Apr 25th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. package lesson04.home_work.level1.loops;
  2.  
  3. public class CharsCounter {
  4. public static void main(String[] args) {
  5. System.out.println(countChars("Hello, world!", 'o'));
  6. System.out.println(countChars("", '1'));
  7. }
  8.  
  9. static int countChars(String s, char ch) {
  10. int count = 0;
  11. char[] charMassive = s.toCharArray();
  12. for (char element : charMassive) {
  13. count = element == ch ? count + 1 : count;
  14. }
  15. return count;
  16. }
  17. }
  18. //output
  19. // "Hello, world!", 'o' => 2
  20. // "", '1' => 0
Add Comment
Please, Sign In to add comment