sunfresh

Count X

Jan 20th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. Count X
  2. Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.
  3.  
  4. Input
  5. On the first line you will be given a string
  6.  
  7. Output
  8. On the only output line you should print the number of lowercase x in it.
  9.  
  10. Sample tests
  11. Input
  12. xxhixx
  13. Output
  14. 4
  15. Input
  16. xhixhix
  17. Output
  18. 3
  19.  
  20. import java.util.Scanner;
  21.  
  22. public class Input {
  23. public static void main(String[] args) {
  24. Scanner scanner = new Scanner(System.in);
  25. String input = scanner.nextLine();
  26. System.out.println(lowerCase(input, 'x'));
  27.  
  28. }
  29. public static int lowerCase(String input, char ch){
  30. int counter = 0;
  31. if(input.length()>0){
  32. if(Character.isLowerCase(input.charAt(0)) && input.charAt(0) == ch){
  33. counter++;
  34. }
  35. counter+=lowerCase(input.substring(1), ch);
  36. }
  37. return counter;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment