I'm trying to make a code that reverse's a string, I know there are more easy ways to do this but I just wanted to know why this code does not work:
public class Reverse
{
/**
* Prints a post backwards to hide its contents.
* @param post the post to be reversed.
*/
public String reverse(String post)
{
String newpost = "";
for (int i = post.length(); i > 0; i++);
{
String letter = post.substring(post.length() - 1, post.length()); // gets the last character of the post
post = post.substring(0,post.length()-1); //removes the last character of the post
newpost = newpost + letter; //adds the last character of the post to newpost
}
return newpost;
}
Result:
Input: How did I ever program without loops?!
Actual: !
Expected: !?spool tuohtiw margorp reve I did woH
Input: That's so backwards!
Actual: !
Expected: !sdrawkcab os s'tahT