3

Can anyone help me write me a Java method that takes a substring of a string, reverses it and prints the outcome?

I'm guessing a StringBuilder is better than String but my issue is StringBuilder.substring() returns a String. To continue using StringBuilder I'd need to create a new StringBuilder object to do the StringBuilder.reverse() part.

Anyone a better solution to this?

    public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("abcdefgh");

    sb = new StringBuilder(sb.subSequence(2, 5));

    sb.reverse(); 
    System.out.println(sb.toString());
}

5 Answers 5

1

You can use this

public String reverseString(String normalString) {
   StringBuilder sb = new StringBuilder();
   for(int i = normalString.length() - 1; i >= 0; --i)
     sb.append(normalString.charAt(i));
   return sb.toString();
 }
Sign up to request clarification or add additional context in comments.

3 Comments

The latter doesn't work. I'd need to create a new StringBuilder object to then reverse.
@heyya99 if you got the solution with my answer please accept this as anser
I think it's overly complicated. I think I could use a String to make it less complicated.
0

You can try something like this:

String s = "Achintya";
List<String> myList = new ArrayList<String>(Arrays.asList(s.split("")));
Collections.sort(myList, Collections.reverseOrder());
System.out.println(myList);

Comments

0

If your desire is to have users input the start and end indicies of the non-reversed String, you could achieve this easily by re-mapping the index values.

Below is a working example of 1 StringBuilder as you asked for:

public static void main(String[] args) {
    final StringBuilder sb = new StringBuilder("abcdefgh");
    final int startIndex = 2;
    final int endIndex = 5;

    sb.reverse();
    final String output = substring(sb.toString(), startIndex, endIndex);
    System.out.println("Result: " + output);
}

//Retrieve the substring of a reversed string
private static String substring(String input, int start, int end) {
    start = map(start, 0, input.length(), input.length(), 0);
    end = map(end, 0, input.length(), input.length(), 0);

    String substring = input.substring(end, start);
    return substring;
}

//Map value from the min,max range, to the new minRange-maxRange range.
private static int map(float value, float min, float max, float minRange, float maxRange) {
    return Math.round(minRange + (maxRange - minRange) * ((value - min) / (max - min)));
}

2 Comments

Thanks but I'm being marked on this and I need it to be as simple as possible. Your solution's complexity would count against me I fear.
It is quite simple really, its just calculating the indices in relation to the reverse. The simplest is yours, but instead of the first String builder you just use: String input = "abcdefgh";
0

Your answer is good enough. If you want to use substring method this too will work,

public static void main(String[] args) 
{
StringBuilder revWord = new StringBuilder("abcdefgh".substring(2,5));
revWord.reverse(); 
System.out.println(revWord);
}

Comments

0

Simply reverse your operations:

StringBuilder sb2 = new StringBuilder("abcdefgh");
sb2.reverse();
System.out.println(sb2.subSequence(sb2.length()-5, sb2.length()-2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.