Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Recursion to replace letter in string

Tags:

java

string

I am full aware that strings are immutable and can't be changed and can be "editabile" - ohhh the controversy! So I am trying to get it so that without the replace() method for strings in java, to implement where a specific char in a string gets switched out with another char. I want to do this as simply as possibly without needing to import any util or use arrays. thus far, I've gotten it to change the character, but it's not returning correctly, or, that is... the string ends.

public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    replace(s, from, to);
}
public static String replace(String s, char from, char to){
    if (s.length() < 1)
        return s;
    if (s.charAt(0) == from) {
        s = to + s.substring(1);
    }
    System.out.println(s);
return s.charAt(0) + replace(s.substring(1, s.length()), from, to);
}
like image 259
user2958542 Avatar asked Feb 25 '14 01:02

user2958542


People also ask

How do you replace a character in a string using recursion?

if(str[0]=='\0') return ; Recursive Call: If the base case is not met, then check for the character at 0th index if it is X then replace that character with Y and recursively iterate for next character.

How do you replace a letter in a string in Java?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

How do you replace a specific letter in a string?

Using 'str.replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


2 Answers

How does this strike you? Fun with tail recursion.

public class Demo {

  public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    System.out.println(replace(words, from, to));
  }

  public static String replace(String s, char from, char to){
    if (s.length() < 1) {
      return s;
    }
    else {
      char first = from == s.charAt(0) ? to : s.charAt(0);
      return first + replace(s.substring(1), from, to);
    }
  }

}

Output:

C:\>java Demo
hello world, i /m / j/v/ progr/m, how /re you tod/y?
like image 80
unigeek Avatar answered Oct 28 '22 13:10

unigeek


 Try this code work for u enjoy it
   public static void main(String[] args) {
    String words = "hello world, i am a java program, how are you today?";
    char from = 'a';
    char to = '/';

    //String ss = words.replace(from, to);
    System.out.println(words);
    String ss = replace(words, from, to);// recieveing String from replace()
    System.out.println("New Replace String is =>  "+ss );
    }

public static String replace(String s, char from, char to){
    if (s.length() < 1)
        return s;
         for(int i=0;i<s.length();i++){
        if (s.charAt(i) == from) {
            s = s.substring(0, i)+to + s.substring(++i);
            System.out.println(s);
            return replace(s, from, to);//calling replace()
        }
    }
    return s;
}

*Output is * New Replace String is => hello world, i /m / j/v/ progr/m, how /re you tod/y?

like image 36
Rishi Dwivedi Avatar answered Oct 28 '22 13:10

Rishi Dwivedi