Using Recursion to Reverse a String - java

I am trying to reverse a String word by word using recursion. (Ex: "Hello my friend" is reversed to "friend my Hello") This is the code I have attempted to write for this method. I have tried multiple similar variations but the output is only ever the first or last word of the String. I believe the part that is "broken" is the first if statement, but I am not quite sure.
public static String reverse (String words) {
Scanner sc = new Scanner(words);
String backwards = "";
if (sc.hasNext()) {
String currentWord = sc.next();
reverse(sc.nextLine());
backwards = backwards + " " + currentWord;
} //end if
else {
backwards = words;
} //end else
return backwards;
}
I am aware that a few similar questions exist, but their answers have not seemed to help me understand my mistake(s).
Thanks!

Instead of using a Scanner, you can make use of an overload of String.split to split words around the first space:
public static String reverse(String words) {
String[] wordArr = words.split(" ", 2); // split into a maximum of 2 Strings
if (wordArr.length > 1) { // If there is more than 1 word
// return the first word (wordArr[0]),
// behind the reverse of the rest of the String (wordArr[1])
return reverse(wordArr[1]) + " " + wordArr[0];
}
return wordArr[0]; // else, just return the one word
}

You shouldn't call nextLine() because your input is all on one line. Your logic is much clearer if you begin by creating a simple helper method, it should take an array of words and a position; from there you can recursively build your desired output with something like
private static String reverse(String[] words, int p) {
if (p + 1 < words.length) {
return reverse(words, p + 1) + " " + words[p];
} else if (p < words.length) {
return words[p];
}
return "";
}
Then your public method is easy to implement, just split the original input on white space and call reverse starting at 0 (remembering to return the result). Like,
public static String reverse(String words) {
return reverse(words.split("\\s+"), 0);
}
And then, I tested it like
public static void main(String[] args) {
System.out.println(reverse("Hello my friend"));
}
Which outputs (as requested)
friend my Hello
Alternatively, you could make that helper take your Scanner instead like
private static String reverse(Scanner sc) {
if (sc.hasNext()) {
String currentWord = sc.next();
if (sc.hasNext()) {
return reverse(sc) + " " + currentWord;
}
return currentWord;
}
return "";
}
And then your public method is
public static String reverse(String words) {
return reverse(new Scanner(words));
}

public static String reverseSentence(String sentence) {
StringBuilder sb = new StringBuilder();
int firstSpace = sentence.indexOf(' ');
if (firstSpace == -1) {
return sb.append(sentence.strip()).append(" ").toString();
}
String secondPart = sentence.substring(firstSpace + 1);
String firstPart = sentence.substring(0, firstSpace);//similar to merger sort
return sb.append(reverseSentence(secondPart)).append(reverseSentence(firstPart)).toString();
}

You throw away the recursion results:
reverse(sc.nextLine());
backwards = backwards + " " + currentWord;
Instead, use this:
backwards = reverse(sc.nextLine());
backwards = backwards + " " + currentWord;
Better yet:
backwards = reverse(sc.nextLine()) + " " + currentWord;

As stated in the comments, you could use a StringBuilder instead of Scanner class.
This example sends the same words, splits them by spaces each time you enter the method and you send the index of the word to be added in the next iteration.
For example:
public class RecursiveReverse {
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
String stringToReverse = "Hello my friend!";
System.out.println(reverse(stringToReverse, stringToReverse.split(" ").length - 1));
}
public static String reverse(String words, int i) {
if (i >= 0) { //If the index of the words is greater or equals the first word
sb.append(words.split(" ")[i]); //We split it and append it to our StringBuilder
sb.append(" "); //We append a space
reverse(words, --i); //We do this again
}
return sb.toString(); //When the above condition doesn't match we return the StringBuilder object as a String (which contains the words reversed)
}
}
Which produces this output:
friend! my Hello
A better method would be passing a String array as parameter so you split only once (when sending the words as an array to the method) the String.
public class RecursiveReverse {
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
String stringToReverse = "Hello my friend!";
String words[] = stringToReverse.split(" ");
System.out.println(reverse(words, words.length - 1));
}
public static String reverse(String words[], int i) {
if (i >= 0) {
sb.append(words[i]);
sb.append(" ");
reverse(words, --i);
}
return sb.toString();
}
}

Do you must use recursion? You can do that without it.
public static String reverse(String words) {
String[] list = words.split(" ");
Collections.reverse(list);
String reversed = String.join(" ", list);
return reversed;
}

You must keep hold of the extracted words between calls in an accumulator. Here is an example.
public static String reverse(String words, String acc){
Scanner sc = new Scanner(words);
if(!sc.hasNext()){
return acc;
}
return reverse(sc.nextLine(), acc) + " " + sc.next();
}
You would call it like this.
reverse("Hello my friend", "");
It's not the most efficient implementation in the world, but yeah... It must work!
If you want a more efficient one, use a StringBuilder as the accumulator.

Related

Juint actual and expected the same but test fails

I am trying to learn Junit4 and do tests for some of my code. Basically, I am reversing the string and keeps the special symbols in place. During the testing, I found this fail that I can't understand.
My unit test fails although the actual value and expected are the same.
Thank you in advance!
Error:
org.junit.ComparisonFailure:
Expected :d1cba hgf!e
Actual :d1cba hgf!e
Code
public final class AnagramsMaker {
public static String reverseWord(String stringToReverse) {
char[] stringToChar = stringToReverse.toCharArray();
int arrayStart = 0;
int arrayEnd = stringToChar.length - 1;
while (arrayStart < arrayEnd) {
if (Character.isLetter(stringToChar[arrayStart]) && Character.isLetter(stringToChar[arrayEnd])) {
char temp = stringToChar[arrayStart];
stringToChar[arrayStart] = stringToChar[arrayEnd];
stringToChar[arrayEnd] = temp;
arrayStart++;
arrayEnd--;
}
else if (Character.isLetter(stringToChar[arrayStart]) && !Character.isLetter(stringToChar[arrayEnd])) {
arrayEnd--;
}
else if (!Character.isLetter(stringToChar[arrayStart]) && Character.isLetter(stringToChar[arrayEnd])) {
arrayStart++;
}
else {
arrayStart++;
arrayEnd--;
}
}
return String.valueOf(stringToChar);
}
public static String createAnagram(String inputString) {
String anagram = "";
String [] arr = inputString.split(" ");
for (String s : arr) {
anagram += reverseWord(s) + " ";
}
return anagram;
}
}
Junit:
#Test
public void createAnagram() {
assertEquals("d1cba hgf!e", AnagramsMaker.createAnagram("a1bcd efg!h"));
}
I tried reproducing it and got this: expected: <d1cba hgf!e> but was: <d1cba hgf!e >. Notice the extra space in the end.
There is a bug in createAnagram method. You are adding extra space after every string but for the last iteration, extra space is getting appended in the end that's causing this issue.
As fix, trim the value before returning as shown below
public static String createAnagram(String inputString) {
String anagram = "";
String [] arr = inputString.split(" ");
for (String s : arr) {
anagram += reverseWord(s) + " ";
}
return anagram.trim();
}
If you are comfortable with java8 & streams, better approach would be to use Collectors.joining method as shown below
public static String createAnagram(String inputString) {
String[] arr = inputString.split(" ");
return Stream.of(arr)
.map(AnagramsMaker::reverseWord)
.collect(Collectors.joining(" "));
}

Convert String to Reverse String Using Recursion in Java

Today I am trying to convert String to reverse String e.g(Cat Is Running into Running Is Cat) word by word not Character
public class ReverseString_ {
public static void reverse(String str) {
String[] a = str.split(" ");
for (int i = a.length - 1; i >= 0; i--) {
System.out.println(a[i] + " ");
}
}
public static void main(String[] args) {
reverse("Cat Is Running");
}
}
The following output is shown:
Running Is Cat BUILD SUCCESSFUL (total time: 0 seconds)
I am trying to convert String into reverse String same as above but through Recursion method but it seems too confusing. and display more errors. Can someone please help me understanding it. Many thanks
public static String reverse_recursion(String str) {
if (str == null)
return null;
else {
String Arry[] = str.split(" ");
int n = Arry.length - 1;
System.out.println(Arry[n] + "");
return reverse_recursion(Arry[n - 1]);
}
}
public static void main(String[] args) {
reverse_recursion("Cat Is Running");
}
This code show following output:
Running
Is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
This code do not print (0) index why? can someone help me to solve this error please
This solution might be helpful. The comments explain the code pretty much.
public static String reverse_recursion(String str) {
String[] arry = str.split(" ", 2); //Split into a maximum of 2 Strings
if (arry.length > 1) { //If there is more than 1 word in arry
//Return the reverse of the rest of the str (arry[1])
//and concatenate together with the first word (arry[0])
return reverse_recursion(arry[1]) + " " + arry[0];
}
return arry[0]; //If less than or equal to 1 word, just return that word
}
This should work:
public static String reverse(String s) {
int idx = s.indexOf(" ");
if (idx < 0) {
// no space char found, thus, s is just a single word, so return just s itself
return s;
} else {
// return at first the recursively reversed rest, followed by a space char and the first extracted word
return reverse(s.substring(idx + 1)) + " " + s.substring(0, idx);
}
}
public static void main(String[] args) {
System.out.println(reverse("Cat Is Running"));
}
You are sending the last element of the Array next time instead of the String without the previously printed String.
Replace your return statement with this it should work.
return reverse_recursion(n==0?null:str.substring(0,(str.length()-Arry[n].length())-1));

Generate palindrome words

So, I have this code which generates palindrome words in a special manner.
1> it joins the word by its reverse(excluding last character).
2> the words which end with repeated alphabets, for example ABB becomes ABBA and not ABBBA and XAZZZ becomes XAZZZAX.
I am already done with the first part. I have just tried second one by extracting last two characters, because to be honest, I don't know how should I do it.
import java.io.*;
class ISC_Q3_2019
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ISC_Q3_2019 ob = new ISC_Q3_2019();
System.out.println("Enter your sentence");
String s = br.readLine();
s=s.toUpperCase();
String words[] = s.split(" ");
int l=words.length;
char ch1=s.charAt(s.length()-1);
String ans=" ", copy=" ", p=" ";
if(ch1=='.'||ch1=='!'||ch1=='?')
{
for(int i=0;i<l;i++)
{
if(ob.isPalindrome(words[i])==true)
{
ans=ans+words[i];
}
else
{
copy=words[i];
words[i]=ob.Reverse(words[i]);
p=copy.concat(words[i]);
ans=ans+p;
}
}
System.out.println("OUTPUT:" +ans.trim());
}
else
System.out.println("Invalid Input!");
}
boolean isPalindrome(String s)
{
s=s.toUpperCase();
int l=s.length();
char ch;
String rev=" ", copy=" ";
copy=s;
for(int i=l-1;i>=0;i--)
{
ch=s.charAt(i);
rev=rev+ch;
}
if(rev.equals(copy))
return true;
else
return false;
}
String Reverse(String s)
{
s=s.toUpperCase();
int l=s.length();
char ch, ch1, ch2;
String r=" ";
for(int i=l-2;i>=0;i--)
{
ch=s.charAt(i);
ch1=s.charAt(l-1);
ch2=s.charAt(l-2);
if(ch1==ch2)
r=r+ch;
else
r=r+ch;
}
return r;
}
}
OUTPUT:
Enter your sentence
The abb is flying.
**OUTPUT:**THE HTABB BAIS IFLYING. GNIYLF
And another part I am concerned is the unmatched spaces.
Here you go
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Palindrome {
public static void main(String... args) {
// Request for input
Scanner reader = new Scanner(System.in);
System.out.println("Enter your sentence...");
// Read the user input
String sentence = reader.nextLine();
// Split the sentence (into tokens) at spaces and special characters using regex
// Keep the special characters where they are and form a List with the split words
List<String> tokens = Arrays.asList(sentence.split("((?<=[\\?,\\. ])|(?=[\\?,\\. ]))"));
// For every token/word, form the palindrome of that and then join them back
String result = tokens.stream().map(s -> formPalindrome(s)).collect(Collectors.joining());
// This is the final result
System.out.println("result: " + result);
reader.close();
}
private static String formPalindrome(String str) {
// Reverse the String
String reversed = new StringBuilder(str).reverse().toString();
// String length
int strLen = reversed.length();
// Compare each character of reversed string with last character of the string until they are different
// When they are different concat the substring with original string
for (int i = 0; i < strLen; i++) {
if (reversed.charAt(i) != str.charAt(strLen - 1)) {
return str + reversed.substring(i);
}
}
return str;
}
}
Here is an idea* that you can build on to get the desired result:
Check the original Strings characters in reverse and count how
many occurences there are
Create a new String from the start of the original String, up
to the end less the count of occurences
Reverse that new String
Concatenate the original String with the new String
So a quickly thrown together example:
int count = 0;
for (int i = s.length() - 1; i > 0; i--) {
if (s.charAt(i) == s.charAt(i - 1)) {
count++;
} else {
break;
}
}
StringBuilder sb = new StringBuilder(s.substring(0, s.length() - 1 - count)).reverse();
System.out.println(s + sb.toString());
Which would give "ABBA" for "ABB" and "XAZZAX" for "XAZZZ".
* It's only an idea and there are probably edge cases etc that aren't catered for but it's merely to give the OP an idea of how to approach it

Recursion - Double each char of a string input and cut of the last char afterwards with one method [duplicate]

I have the following problem.
The recursive method public static String doSomeMagic("Test") should return:
TTeesstt
TTeess
TTee
TT
I've implemented this behaviour already like this:
public static String rowFunction(String s) {
String toReturn = new String();
if (!s.isEmpty()) {
toReturn = String.valueOf(s.charAt(0));
toReturn += toReturn + rowFunction(s.substring(1));
}
return toReturn;
}
public static String doSomeMagic(String s) {
String toReturn = new String();
if (!s.isEmpty()) {
toReturn = rowFunction(s) + "\n" + doSomeMagic(s.substring(0, s.length() - 1));
}
return toReturn;
}
How can one achieve this with just one function? Any ideas?
I noticed you wanted to do this without a loop and in one function call. You can probably clean this up a lot more. Here it is:
public static String doSomeMagic(String s) {
if (!s.isEmpty()) {
StringBuffer sb = new StringBuffer();
return sb.append(s.replaceAll("(\\S)", "$1$1"))
.append("\n")
.append(doSomeMagic( s.replaceAll(".$", "") )
.toString();
}
return "";
}
To do it in one function, just iterate over the string rather than calling another recursive function.
public static String doSomeMagic(String s) {
String doubled = new String();
if (s.length() == 0) return s;
for(int i=0;i<s.length();i++)
doubled += s.substring(i,i+1) + s.substring(i,i+1)
return doubled + "\n" + doSomeMagic(s.substring(0, s.length()-1));
}
Quick solution could be like
testMethod(string ip){
if(ip.length()==1){
ip=ip.toUppercase();
}
For(int i=0;i<ip.length()-1;i++){
System.out.print(ip.charAt(i)+""+ip.charAt(i));
}
if(ip.length()>1){
System. out. println();
testMethod(ip.substring(1));
}
}
Haven't tested it... But should work fairly

java method returning a modified string

I'm trying to create a method that will accept 2 strings as arguments. The first string will be a phrase, the second also a prhase. What I want the method to do is to compare both strings for matching chars. If string 2 has a char that is found in string 1 then replace string 2's instance of the char with an underscore.
Example:
This is the input:
phrase1 = "String 1"
phrase2 = "Strone 2"
The output string is called newPhrase and it will have the string built from the underscores:
newPhrase = "___one 2"
Its not working for me I am doing something wrong.
public class DashedPhrase
{
public static void main(String[] args)
{
dashedHelp("ABCDE","ABDC");
}
public static String dashedHelp(String phrase1, String phrase2)
{
String newPhrase = "_";
for(int i = 0; i < phrase.length(); i++)
{
if(phrase.charAt(i) == phrase2.charAt(i))
{
newPhrase.charAt(i) += phrase2.charAt(i);
}
}
System.out.print(newPhrase);
return newPhrase;
}
}
To make it easier for you to understand, you can use StringBuilder and its method setCharAt().
Notice the i < phrase1.length() && i < phrase2.length() in the condition for the for loop. This is to make sure you don't get any ArrayIndexOutOfBounds exception.
public static void main(String[] args)
{
System.out.println("ABCDE");
System.out.println("ABDC");
dashedHelp("ABCDE","ABDC");
System.out.println();
System.out.println();
System.out.println("String 1");
System.out.println("Strone 2");
String phrase1 = "String 1";
String phrase2 = "Strone 2";
dashedHelp(phrase1, phrase2);
}
public static String dashedHelp(String phrase1, String phrase2)
{
StringBuilder newPhrase = new StringBuilder(phrase1);
for(int i = 0; i < phrase1.length() && i < phrase2.length(); i++)
{
if(phrase1.charAt(i) == phrase2.charAt(i))
{
newPhrase.setCharAt(i, '_');
}
}
System.out.print(newPhrase);
return newPhrase.toString();
}
Output:
ABCDE
ABDC
__CDE
String 1
Strone 2
___i_g_1
newPhrase.charAt(i) doesn't let you replace a character, it just returns it. Java's Strings are immutable. I you want to change it you should use StringBuilder. Look into the replace(int start, int end, String str) method.
Since you need to return a string that has the same length as phrase2, you need to iterate over each character of phrase2, and replace the matching characters of both phrases. And, of course, if phrase2 is longer than phrase1, you need to include the remaining characters in the answer. You can try this:
public static String dashedHelp(String phrase1, String phrase2) {
String ans = "";
String subChar = "_";
int i;
for(i = 0; i<phrase2.length(); i++) {
if(i<phrase1.length() && phrase1.charAt(i) == phrase2.charAt(i))
ans += subChar;
else
ans += phrase2.charAt(i);
}
return ans;
}
Hope it helps
Of course, if you need to output phrase1 with underscores in the places where phrase2 has equal characters, you can interchange phrase2 with phrase1 in the above code.
Testing it
The complete class would look like this:
public class MyClass {
public static String dashedHelp(String phrase1, String phrase2) {
// The method code goes here
}
public static void main(String[] args) {
System.out.println(dashedHelp("String 1", "Strone 2"));
}
}
The output of this program is ___o_e_2. This matches (approximately) your desired output.
The code in the example won't even compile.
newPhrase.charAt(i) += phrase2.charAt(i);
That's a bad assignment. It's the same as writing
newPhrase.charAt(i) = newPhrase.charAt(i) + phrase2.charAt(i);
but the expression on the left side of the '=' isn't something to which you can properly assign a value.

Categories