Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: String Recursive Method

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post String Recursive Method

    I was given the prompt below and while I have made my program count the number of times the word "hi" appears in the String given to the method, I'm not sure what to do about having the code ignore cases where the word "hi" has an x before it. Below is my prompt and below that, my code thus far. I know I need to use certain String methods but not exactly sure which one is the correct one I need for my problem nor where it needs to be placed. Any help is appreciated

    Given a string, compute recursively the number of times lowercase "hi" appears in the string, however, do not count "hi" that has an 'x' immediately before them.
    countHi2("ahixhi") → 1
    countHi2("ahibhi") → 2
    countHi2("xhixhi") → 0
    Method Header: public static int countHi2(String str)

    Here is the Code:
    public class RecursionProblem {
     
    	public static void main(String[] args) {
     
    		System.out.println("Number of times the word hi appears with the exception of those with an x before it: " + countHi2("hixhihi"));
     
    	}//end main
    public static int countHi2(String str) {
     
                    //base case
    		if(str == null)
    			return 0;
     
    		if(str.length() < 2)
    			return 0;
     
    		String first2Letters = str.substring(0, 2);
    		if(first2Letters.equals("hi"))
    			return 1 + countHi2(str.substring(2));
     
    		return countHi2(str.substring(1));
     
    	}//end countHi2 method
    }//end class

    For reference, I added this to the code:
     else if (str.length() >= 3 && str.charAt(0) == 'x')
        return countHi2(str.substring(3));
    Here:
    public static int countHi2(String str) {
     
    		if(str == null)
    			return 0;
     
    		if(str.length() < 2)
    			return 0;
     
    		String first2Letters = str.substring(0, 2);
    		if(first2Letters.equals("hi"))
    			return 1 + countHi2(str.substring(2));
     
    		else if (str.length() >= 3 && str.charAt(0) == 'x')
    			return countHi2(str.substring(3));
     
    		return countHi2(str.substring(1));
     
    	}//end countHi2 method
    and the program counted only the "hi" without an x before it as desired.
    Last edited by HyperRei; October 24th, 2020 at 05:02 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String Recursive Method

    Can you describe the algorithm for the method in English?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: String Recursive Method

    Quote Originally Posted by Norm View Post
    Can you describe the algorithm for the method in English?
    Yes I can. The only line within main, as you can see, just prints out the output of the countHi2 method. As to what the countHi2 method does, the first two if statements are supposed to be base cases. The line:
    String first2Letters = str.substring(0, 2);
    this is so I can check for the "hi" should it come up and add 1 to the count. I add 1 to the count here:
    return 1 + countHi2(str.substring(2));
    Finally, I end the method off with this:
    return countHi2(str.substring(1));
    While it counts the number of "hi" it is given into the method through the System.println:
    System.out.println("Number of times the word hi appears with the exception of those with an x before it: " + countHi2("hixhihi"));
    I don't know how to make it ignore the cases where "hi" has an x before it. So here, it is given, "hixhihi" where the count should be 2. While we can visually see three hi we ignore the one with the x before it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String Recursive Method

    Finally, I end the method off with this:
    That doesn't describe what purpose of that statement is.
    It appears that the method moves through the String one char at a time. If it finds "hi" it adds one to the count and advances 2 char.
    What should it do if it finds an "x"?

    Note: the startsWith method would be more direct way to find "hi" than substring and equals
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: String Recursive Method

    I honestly forget what that line did...but yes you are correct that is exactly what it does. When it finds it x before hi, it simply does not count it. It should only count hi should it appear and if it does not have an x before it. Example string: hixhihi. This would equal 2 and ignore the middle hi as there is an x before it.

Similar Threads

  1. confused on this recursive method example
    By chakana101 in forum Object Oriented Programming
    Replies: 2
    Last Post: April 18th, 2014, 09:33 AM
  2. Replies: 4
    Last Post: December 9th, 2013, 10:40 AM
  3. Help in understanding this recursive method
    By jameschristopher06 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 20th, 2012, 01:23 PM
  4. help with recursive method
    By mflb94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 06:30 PM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM

Tags for this Thread