Program that gets input and reverse the string

923 views Asked by At

My code is suppose to use loops only, I am suppose to make a string reverse and return that reverse value. I have a problem return the reverse value and I have no clue how to do it.`

public static String reverseString(String str){

for(int i =str.length()-1;i>=0;i--)



return str.charAt(i);// This wont return the value, gives me a error
                             // any tips?

}
2

There are 2 answers

2
Dyrborg On BEST ANSWER

Assuming you use Java you can change your code to this:

public static String reverseString(String str){

    StringBuilder sb = new StringBuilder();

    for(int i =str.length()-1;i>=0;i--){

        sb.append(str.charAt(i));
    }

    return sb.toString();

}

The change is that you create a StringBuilder object. The code you posted tries to return the first char you extract from your string which is not what you want. You want to build the string in reverse, so in each iteration you add the next reversed char to your StringBuilder, and when the loop terminates, you return the string representation of the StringBuild by using toString().

You can do this implementation smarter and faster, but I assume that this task is part of a learning process, so it is more important that you understand why your code should not place a return statement in a loop, when you actually want the loop to iterate multiple times and save each intermediate result before returning anything.

Documentation:

http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

UPDATE:

Based on ohiodoug's comment about StringBuilder it would be appropriate to also show how it is done without the StringBuilder class:

import java.lang.StringBuilder;

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

        String resultSimple = simpleReverseString("Hello World!");
        String resultSB = betterReverseString("Hello World!");
        System.out.println(resultSimple);
        System.out.println(resultSB);
        //Prints:
        //!dlroW olleH
        //!dlroW olleH
    }

    //Simple string concatenation. 
    //Uses the += operator.
    //Since s is already a String we don't need to use
    //toString() on it.
    public static String simpleReverseString(String str){

        String s = "";

        for(int i =str.length()-1;i>=0;i--){
            //s += str.charAt(i) is equal to
            //s = s + str.charAt(i)
            s += str.charAt(i);
        }
        return s;
    }        

    //Same as simpleReverseString, but uses the StringBuilder class
    public static String betterReverseString(String str){

        StringBuilder sb = new StringBuilder();

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

Updated answer:

public static String reverseString(String str){
     char[] temp = new char[str.length()];
     for (int i = 0; i < str.length(); i++) {
         temp[i] = str.charAt(str.length() - (i + 1));
     }
     return new String(temp);
}

Good luck!