-3

I want to reverse a String, I know there are thousands of examples, but I wanted to do the job by myself and I created this, but it doesn't work properly.

    public String ReverseString(String str){
        str = str.toLowerCase();

        char normalArr[] = str.toCharArray();
        char reversedArr[] = new char[str.length()];

        for (int i=str.length()-1; i<=0; i--){
            int count = 0;
            reversedArr[count] = normalArr[i];
            count++;
        }

        String retValue = new String(reversedArr);
        return retValue;
    }
Mat
  • 202,337
  • 40
  • 393
  • 406
Cristian
  • 359
  • 3
  • 6
  • 12
  • 2
    Your code is lacking debug statements -- println statements that tell you the value of variables at different locations of code as the program is running. Better to learn this technique than to come to SO for a handout. – Hovercraft Full Of Eels Jul 07 '12 at 05:30
  • @HovercraftFullOfEels Or the breakpoint/debugger is a very useful tool to step through your code and see how it is executing at every line. It will also let you track variables to see how they change during operation. (Any modern IDE anyway) – David B Jul 07 '12 at 05:41
  • Thanks for your insight, but I'm just learning to code using Java. I tried to do this as a challenge, if I wanted to do it the safe way, I would have done something like: public String ReverseStringEasy(String str){ StringBuffer sBuffer = new StringBuffer(str); return sBuffer.reverse().toString(); } – Cristian Jul 07 '12 at 05:46
  • That's true, I have a lot to learn, I'll try to investigate Eclipse's debug functions. Thanks. – Cristian Jul 07 '12 at 05:47
  • Also, please keep in mind that this approach to reverse breaks down whenever you are dealing with strings that have codepoints outside the basic plane. You will note that `StringBuffer.reverse()` has the additional logic to be surrogate-pair safe: http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html#reverse() – Dilum Ranatunga Jul 07 '12 at 06:11

3 Answers3

1

Look at the condition of your loop - it should be i >= 0
In addition - your solution allocates an unnecessary array (there are algorithms that perform this with a single array, not yours , though) - I think you should strive to code your programs to be with good performance.

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27
1

In addition to @zaskes answer, you want int count = 0; before the loop, not inside.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
0

Here's an implementation that does the reverse logic:

StringBuffer reversed = new StringBuffer(str.length());
int loc = str.length();
while (loc > 0) {
  char c1 = str.charAt(--loc);
  if (Character.isLowSurrogate(c1)) {
    assert loc >= 0;
    char c2 = str.charAt(--loc);
    reversed.append(c2);
  }
  reversed.append(c1);
}
return reversed.toString();

If you consider the use of StringBuffer for simple appends cheating, you can replace that with:

char[] reversed = new char[str.length()];
int next = 0;
...
reversed[next++] = c1; // instead of .append(c1)
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52