0

I managed to reverse a String making it a char array, also with .reverse method on a Stringbuilder, managed to do it with another String and a for loop (I read that this isn't a good idea because it hurts the performance), but i cant figure out how to reverse a string with a Stringbuilder and for loop. It gives me error - "The left-hand side must be a variable" . Also tried to set a length for the Stringbuilder, but looks like I am missing something. I tried to set indexes outside the brackets of the Stringbuilder, because they are for parameters, but I am just guessing how to do it, tried .append as well.

public class Task1 {

    public static void main(String[] args) {

        String str = "Reverse me";
        StringBuilder printStr = new StringBuilder();
        int j = 0;

        for (int i = str.length()-1; i > 0; i--) {
            printstr(j) = str[i];

        }
    }

}
2

3 Answers 3

4

I think you are on the right lines, just a few things I'd change: Firstly we have the ability to specify the length of the StringBuilder because we know it is going to be the same length as the String we are reversing.

It is not necessary to loop through the entire array, just half the array will suffice performing operations which are essentially just switching the characters. So the right-most character is now the left-most character, this saves a little computation.

public static void main(String[] args) throws IOException {
    String str = "Reverse me";
    StringBuilder printStr = new StringBuilder();
    printStr.setLength(str.length());

    for(int i =0; i<str.length()/2;i++){
        char left = str.charAt(i);
        final int fromRight = str.length() - i - 1;
        printStr.insert(i, str.charAt(fromRight));
        printStr.insert(fromRight, left);
    }
}

Here is a visual representation of how to character switching essentially works: More information can be found here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

OK, so you made multiple mistakes, you are trying to access indexes by using parenthesis, you are not iterating over whole string as your loop condition is i > 0 and it should be i >= 0, and your loop is just wrong (disregard parenthesis), you can combine append() and charAt() methods to do what you need:

    for (int i = str.length()-1; i >= 0; i--) {
        printStr.append(str.charAt(i));
    }

We go from last index of your string up to index 0, so we start from the end of the string, and we append each character to our StringBuilder. In the end you have your reversed string.

Comments

0
    public class HelloWorld {
    public static void main(String[] args) {

        System.out.println(reverseString("Example"));
    }

    public static String reverseString(String str) {
        StringBuilder builder = new StringBuilder();

        for (int i = str.length() - 1; i >= 0; i--) {
            builder.append(str.charAt(i));
        }
        return builder.toString();
    }
}

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.