sunfresh

Count Hi

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