Results 1 to 11 of 11
  1. #1
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default help with returning the string in reverse

    I'm somewhat new to java and I'm trying to get a string to return in reverse which takes a String parameter, str, for example the word "hello", I want to be able to output "olleh" the code I used was :
    String result = " ";

    for (int k = 0; k < string.length; k++);
    result(length-1-i) = str(i);
    return result;

    I'm not getting the output I want so if someone can help me with this I'll really appreciate it.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you ended your for loop without doing anything by adding a semi-colon at the end. remove the semi-colon and use braces like this:

    Java Code:
    for (int k=0; k<string.length; k++) {
        result(length-1-i) = str(i);
        return result;
    }

    i'm assuming you declared 'i' and the method result(int) somewhere else

  3. #3
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    Yea, it starts like this but I need to declare i or can I replace i with k?
    public String reverseString(String str)

    for (int k=0; k<string.length; k++)
    result(length-1-i) = str(i);
    return result;

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    just use the String.reverse() method in the String class

  5. #5
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    so I dont have to type in that for loop?

  6. #6
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    yes so that your life would be easier

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by RichersooN View Post
    just use the String.reverse() method in the String class
    Please don't talk nonsense; there is no reverse() method in the String class. If you want to help make sure your answer is at least correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    /*
    Java String Reverse example.
    This example shows how to reverse a given string
    */

    public class StringReverseExample {

    public static void main(String args[]){
    //declare orinial string
    String strOriginal = "Hello World";
    System.out.println("Original String : " + strOriginal);

    /*
    The easiest way to reverse a given string is to use reverse()
    method of java StringBuffer class.
    reverse() method returns the StringBuffer object so we need to
    cast it back to String using toString() method of StringBuffer
    */

    strOriginal = new StringBuffer(strOriginal).reverse().toString();

    System.out.println("Reversed String : " + strOriginal);
    }

    }

    /*
    Output of the program would be :
    Original String : Hello World
    Reversed String : dlroW olleH
    */

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Hurray! Spoonfeeding again.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    andy9507 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    This code here works great in any situation.


    public String reverse(String str) {
    char[] sequence = str.toCharArray();
    char[] reversed = new char[sequence.length];
    int j = 0;
    for (int i = sequence.length - 1; i >= 0; i--) {
    reversed[j] = sequence[i];
    j++;
    }
    String returnString = new String(reversed);
    return returnString;
    }
    }

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by RichersooN View Post
    just use the String.reverse() method in the String class
    you mean StringBuffer...

    Java Code:
    String reverse = new StringBuffer(string).reverse().toString();

Similar Threads

  1. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 08:10 PM
  2. Reverse A String Without Allocating A New String
    By marco.c84 in forum Advanced Java
    Replies: 10
    Last Post: 03-22-2011, 05:39 AM
  3. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 02:31 AM
  4. reverse string split
    By Fittersman in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2010, 12:29 AM
  5. reverse a string with a while loop...
    By OptimusPrime in forum New To Java
    Replies: 9
    Last Post: 12-28-2009, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •