2 Replies - 1877 Views - Last Post: 03 April 2012 - 10:49 AM Rate Topic: -----

#1 edwardo388   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-March 12

Extract string b from string a using recursion

Posted 29 March 2012 - 10:41 AM

I have a project in my java class that asks me to extract several letters from a string using another string that a user can input. I think I'm close, I just can't seem to wrap my head around recursion just yet. IE: 'asdfasdfasdf' is inputted from the user, if we set 'f' as the filter, 'asdasdasd' would output.

import java.util.Scanner;

public class project {

public static String filter(String s, String rem){
    String newStr=s;
       if(s.substring(0, s.length()).equals(rem)) {
       return null;
    } 
    else {
 
    if(s.length() == 1) {
        return newStr;
    }
    }
    return filter(s.substring(1), newStr);
}

 

public static String reverse(String s){
        if (s==null){   
        return null; 
            }   
        int l=s.length();   
        if (l==1){   
        return s;   
        }
        String lastChar = s.substring(s.length()-1,s.length());
        String remainingString = s.substring(0, s.length() -1);
        s = lastChar + reverse(remainingString);
        return s;

}

    public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in); //scanner input
    System.out.print("Input a string: ");
    String str = kbd.next(); //input for string
    System.out.println("What would you like to filter?");
    String rem = kbd.next(); //input for what the user wants to be filtered
    System.out.println("Your input was: " +str + " you are removing: " +rem);
    System.out.println("");
    System.out.println("Filtered, your string is now: ");
    System.out.println("");
    //run the filter
    System.out.println(filter(str,rem));
    
    
    }
}



If someone could point me in the right direction that would be AWESOME!

Is This A Good Question/Topic? 0
  • +

Replies To: Extract string b from string a using recursion

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Extract string b from string a using recursion

Posted 29 March 2012 - 02:09 PM

This is really using recursion to use recursion because there is no real benefit of coding something like that

String applyFilter(String sentence, char filter) {
     if(sentence.length() == 0)
        return "";

     char first = sentence.charAt(0);
     if(first == filter)
        return applyFilter(sentence.subString(1), filter);
     
     return String(first) + applyFilter(sentence.subString(1), filter);
}


Was This Post Helpful? 0
  • +
  • -

#3 edwardo388   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-March 12

Re: Extract string b from string a using recursion

Posted 03 April 2012 - 10:49 AM

View Postpbl, on 29 March 2012 - 02:09 PM, said:

This is really using recursion to use recursion because there is no real benefit of coding something like that

String applyFilter(String sentence, char filter) {
     if(sentence.length() == 0)
        return "";

     char first = sentence.charAt(0);
     if(first == filter)
        return applyFilter(sentence.subString(1), filter);
     
     return String(first) + applyFilter(sentence.subString(1), filter);
}



This only works for a single character though, I've had that part down for awhile. What I need to do is extract an entire string from a string.. so if I typed 'abcdabcdabcd' and i was filtering 'abc', the output would be 'ddd'
import java.util.Scanner;


public class HW11 {
    
    public static String filter(String s, String rem) {
     if(s.length() == 0)
        return "";
     
     if (rem.length()==1){

     char first = s.charAt(0);
     if(first == rem.charAt(0))
        return filter(s.substring(1), rem);
     
     return first+ filter(s.substring(1), rem);
        }
     
    }


 

public static String reverse(String s){
        if (s==null){   
        return null; 
            }   
        int l=s.length();   
        if (l==1){   
        return s;   
        }
        String lastChar = s.substring(s.length()-1,s.length());
        String remainingString = s.substring(0, s.length() -1);
        s = lastChar + reverse(remainingString);
        return s;

}

    public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in); //scanner input
    System.out.print("Input a string: ");
    String str = kbd.next(); //input for string
    System.out.println("What would you like to filter?");
    String rem = kbd.next(); //input for what the user wants to be filtered
    System.out.println("Your input was: " +str + " you are removing: " +rem);
    System.out.println("");
    System.out.println("Reversed, your string is: ");
    //run the reverse method
    System.out.println(reverse(str));
    System.out.println("Filtered, your string is: ");
    //run the filter method
    System.out.println(filter(str,rem));
    
    
    }
}




That's where I'm currently at, the reverse method works, but I can't get my head around this filter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1