Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Count Hi
- Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string.
- Input
- On the first line you will be given the string.
- Output
- On the only output line you should print the number of hi-s.
- Sample tests
- Input
- xxhixx
- Output
- 1
- Input
- xhixhix
- Output
- 2
- import java.util.Scanner;
- public class CountHi {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- System.out.println(countHi(input));
- }
- static int countHi (String string){
- if(string.length()<2){
- return 0;
- }
- if(string.substring(0,2).equals("hi")){
- return 1+countHi(string.substring(2));
- }
- return countHi(string.substring(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment