Results 1 to 10 of 10
  1. #1
    mlotfi is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default How to reverse a string, skiping numbers , and ' ?

    Hi,

    If we have :

    String source = "85.34% of all, the application are wrong, it's normal"

    How to reverse it to :

    "85.34% fo lla, eht noitacilppa era gnorw, ti's lamron"

    Thanks

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

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

    Default

    And even before the code think about how you rather than the computer would get the output from the input. Does it involve splitting the original string up in some way? And doing something to the component parts?

    The thing is, you have to be comprehensive and specific with your "recipe" - be able to describe the process you would use completely. This might involve being explicit about things that you are normally unconscious of: like "What is a number?"

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

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

    Default

    More consciousness raising: Be Forthright When Cross Posting To Other Sites at JavaRanch (applies generally, not just to that site)

  6. #6
    ranjiths's Avatar
    ranjiths is offline Member
    Join Date
    Sep 2009
    Location
    India
    Posts
    19
    Rep Power
    0

    Smile

    Suppose the sentence to be reserved is stored in str

    Java Code:
    String temp;
    int length=str.length() - 1;
    char ch;
    for(int i=0;i<=length;i++)
    {
    ch=str.charAt(i); 
    if (ch==" ")
    {
    reverse(temp);
    temp="";
    System.out.print(" ");
    }
    else
    str=str.concat(ch);
    }
    
    
    void reverse(String s)
    {
    int length2=s.length()-1;
    for(int j=length2;j>=0; j++)
    {
    System.out.print(s.charAt(j));
    }
    }
    Hope that your question is solved.
    All that a fellow passenger on this earth expects from you is your loving words and support. Never talk in a way that hurts others or insults them. Sarvejana Sukhinobavanthu.

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

    Default

    Quote Originally Posted by ranjiths View Post
    Suppose the sentence to be reserved is stored in str

    Java Code:
    String temp;
    int length=str.length() - 1;
    char ch;
    for(int i=0;i<=length;i++)
    {
    ch=str.charAt(i); 
    if (ch==" ")
    {
    reverse(temp);
    temp="";
    System.out.print(" ");
    }
    else
    str=str.concat(ch);
    }
    
    
    void reverse(String s)
    {
    int length2=s.length()-1;
    for(int j=length2;j>=0; j++)
    {
    System.out.print(s.charAt(j));
    }
    }
    Hope that your question is solved.
    Does your code even compile? I think it doesn't so don't post it as if it were a solution to the question.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Hope that your question is solved.
    And even if it doesn't compile, handing over solutions like that does next to nothing to help the original poster learn how to problem solve, debug, write code, and any other aspect that is essential to the learning process - all it does is help them cheat. Just this person's .02 though

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

    Default

    The key to this, like others have said above, is getting the logic of how you would do this outside of programming, and then implementing that logic in your program by using the String class.

  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

    Quote Originally Posted by doWhile View Post
    And even if it doesn't compile, handing over solutions like that does next to nothing to help the original poster learn how to problem solve, debug, write code, and any other aspect that is essential to the learning process - all it does is help them cheat. Just this person's .02 though
    I agree entirely with this post. Do not spoonfeed! Ranjiths, please verify that your code is working before posting. If it is, don't post it! Give them a small nudge in the correct decision. Spoonfeeding more incorrect informastion will not help you.

    Whenever you go to post something, stop for a second and think. Google if needed, do research and try to see if you can confirm what you thought before giving help.


    @op: It may be helpful to create a method which reverses a single 1 word string. From there split should come in handy.

Similar Threads

  1. help with returning the string in reverse
    By thorobred in forum New To Java
    Replies: 10
    Last Post: 04-24-2011, 01:21 AM
  2. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 09:10 PM
  3. 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
  4. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 03:31 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
  •