Results 1 to 18 of 18
  1. #1
    Neeer is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Post How to reverse a string?

    IF I input a string say: "He is good"
    the out will be: "eH si doog"
    I can only functions such a : charAT(int) ; length()
    The program should be done using for loop. I am confused.Please Help:confused:

  2. #2
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    63
    Rep Power
    0

    Thumbs up

    one way:
    take the string as character array
    c[]=[h,e, ,i,s, ,g,o,o,d]
    now read each character from end concat each character into a string.
    String s=new String();
    s.concat(c[end]);

    second way:
    in StringBuffer there is method called reverse()
    by using this we can reverse the string

    i hope you can understand:rolleyes:

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

    Default

    here's another way:
    Java Code:
    String str = "he is good";
    String[] temp;
    String reversedStr = "";
    String delimiter = " ";
    temp = str.split(delimiter);
    // temp [string array] now stores three values, "he", "is", and "good"
    for (String s:temp) {
      reversedStr += new StringBuffer(s).reverse().toString() + delimiter;
    }
    // reversedStr = "eh si doog";

  4. #4
    Neeer is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Unhappy

    I cannot use array too or any other functions that i did not mention.

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

    Default

    oh you have to do it manually i see. then you need to work out your logic

    - while (index doesnt equal length of string) {
    - X = start index;
    - if charAt(index) = white space {
    index X to here = new word
    reverse it
    }
    - loop }

    "length of string"
    use String.length

    "index X to here"
    when you have two indexes e.g. 0 and 2, you can use subsequence to get all the characters between two indexes

    "reverse it"
    take the last index first and copy it to a temporary variable, loop through the sub-word until the length of it is finished
    Last edited by ozzyman; 03-25-2011 at 04:33 PM.

  6. #6
    Neeer is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Can you give a sample code please...

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

    Default

    i would except this looks like your homework :). you wont learn unless you try it yourself, so i've given u some logic to help u out. try to write it and try to compile it, if you get an error tell us what the error is

  8. #8
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    String sample="he is good";
    for(int x=sample.length()-1;x>=0;x--){
    System.out.print(sample.charAt(x));
    }
    Keep Moving Forward

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Quote Originally Posted by akiravelmont View Post
    one way:
    take the string as character array
    c[]=[h,e, ,i,s, ,g,o,o,d]
    now read each character from end concat each character into a string.
    String s=new String();
    s.concat(c[end]);

    doog ton si sihT

    tI dluohs eb ekil siht

    Read the original post.

    Ditto nap_patague, and I agree with ozzyman about what is helpful and what is not. We shouldn't beg for code and we shouldn't encourage the beggars.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried to solve this? You got advice in this thread, some incorrect as pbrock mentioned but they are showing the idea. Show what kind have work you have done and we will let you know what to focus on.

  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 nap_patague View Post
    Java Code:
    String sample="he is good";
    for(int x=sample.length()-1;x>=0;x--){
    System.out.print(sample.charAt(x));
    }
    this isn't the correct solution btw, he wants each word to be reversed not the whole sentence

    i.e. the output should be "eh si doog" NOT "doog si eh"

    then again it would still work if he separated each word before he runs this type of loop

  12. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Neeer View Post
    IF I input a string say: "He is good"
    the out will be: "eH si doog"
    I can only functions such a : charAT(int) ; length()
    The program should be done using for loop. I am confused.Please Help:confused:
    Java Code:
    char c=yourString.charAt(yourString.length());
    String reverseString;
    for(int i=0; i<yourString.length(); i++) {
    c=yourString.charAt(yourString.length()-i);
    reverseString.concat(c.toString());
    }

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please don't spoon feed code. Especially when the code you are giving is incorrect, and has already been provided, and the reason why it was wrong has been explained.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Quote Originally Posted by Solarsonic View Post
    Java Code:
    char c=yourString.charAt(yourString.length());
    String reverseString;
    for(int i=0; i<yourString.length(); i++) {
    c=yourString.charAt(yourString.length()-i);
    reverseString.concat(c.toString());
    }
    Please compile, run and test any code before posting. That'll save you from the embarrassment of posting erroneous code that doesn't come close to meeting the requirement.

    And when you do manage to develop a code that works, don't spoonfeed -- that deprives the questioner of a learning opportunity.

    db

  15. #15
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Please compile, run and test any code before posting. That'll save you from the embarrassment of posting erroneous code that doesn't come close to meeting the requirement.

    And when you do manage to develop a code that works, don't spoonfeed -- that deprives the questioner of a learning opportunity.

    db
    I don't see anything wrong with my code?

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    the result should be: "eH si doog"
    not : "doog si eH"

    Also, don't spoonfeed.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Solarsonic View Post
    I don't see anything wrong with my code?
    For one, per pbrockway and others:

    Quote Originally Posted by pbrockway2 View Post
    Ditto nap_patague, and I agree with ozzyman about what is helpful and what is not. We shouldn't beg for code and we shouldn't encourage the beggars.

  18. #18
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Sorry guys I misread OP's post and ill try not to spoonfeed.

Similar Threads

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

Posting Permissions

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