Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: How to invert a word String

  1. #1
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Post How to invert a word String

    import java.io.*;
     
    /**
     * JavaProgrammingForums.com
     */
    public class WordInverter {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
        public static void main(String[] args) throws IOException
        {
            String strInv = null, word;
            System.out.print("Enter a word : ");
            word = br.readLine();
            strInv = invert(word);
     
            System.out.println(strInv);
        }
     
        public static String invert(String s) {
            String temp = "";
            for (int i = s.length() - 1; i >= 0; i--)
                temp += s.charAt(i);
            return temp;
        }
    }
    Cheers,

    Truffy

  2. The Following User Says Thank You to Truffy For This Useful Post:

    JavaPF (July 22nd, 2009)


  3. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to invert a word String

    Or you can use the built in feature of the java.lang.StringBuilder

            final StringBuilder stringBuilder = new StringBuilder("MyString");
            System.out.println(stringBuilder.reverse());
    // Json

  4. The Following User Says Thank You to Json For This Useful Post:

    JavaPF (July 22nd, 2009)

  5. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to invert a word String

    Hello Truffy,

    Thank you very much for that submission. I edited it slightly just to clean it up.

    Keep up the good work
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #4
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: How to invert a word String

    Quote Originally Posted by JavaPF View Post
    Hello Truffy,

    Thank you very much for that submission. I edited it slightly just to clean it up.

    Keep up the good work
    Thanks JavaPF and Json.

    Cheers,

    Truffy

  7. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to invert a word String

    take two pointer and start swapping .

  8. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to invert a word String

    Quote Originally Posted by niraj View Post
    take two pointer and start swapping .
    Would you like to elaborate on this point please. Since Java doesn't use pointers, this is not possible. If this were a C or C++ forum then it would be acceptable, so please explain what you mean.

    Thanks,
    Chris

  9. #7
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: How to invert a word String

    Quote Originally Posted by Freaky Chris View Post
    Would you like to elaborate on this point please. Since Java doesn't use pointers, this is not possible. If this were a C or C++ forum then it would be acceptable, so please explain what you mean.

    Thanks,
    Chris

    Yup, I agree with Cris. I think C and C++ uses pointers. Java doesn't uses pointers.

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to invert a word String

    well, technically all object variables are pointers...

  11. #9
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to invert a word String

    hi i think truffy's simple program is far more usefull than the preDefined class
    String builder.reverse in java.

    truffy's program can accept data(Strings of characters) and it will reverse it
    unlike String builder it only accepts arguments
    i supposed it was made with a VOID type of return

  12. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to invert a word String

    I'm not entirely sure what you are on about here, but the StringBuilder will accept strings, in fact whenever you write code in Java like this.

    final String myString = "some cool string here " + "with some more data here"

    That will in the compiled Java byte code be something like this.

        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("some cool string here ");
        stringBuilder.append("with some more data here");

    A wee bit of reading for you is available at StringBuilder (Java Platform SE 6)

    If you would like it as a method that takes a string and returns a string, this is what you could do.

        public String reverse(final String input) {
            return new StringBuilder(input).reverse().toString();
        }

    // Json
    Last edited by Json; August 20th, 2009 at 08:50 AM.

  13. #11
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to invert a word String

    Quote Originally Posted by helloworld922 View Post
    well, technically all object variables are pointers...
    they are references, and we cannot deal with the pointers / references ourself. Java does all of that and so we have no input on it, unlike C where you have to do it all yourself.

  14. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to invert a word String

    they behave more like C pointers than C references cause they're not static (thank goodness!). But yes, this makes absolutely no sense in Java but does in C (well, at least it compiles).
    SomeObject a = new SomeObject();
    a++;

    SomeObject *a = new SomeObject();
    a++;

  15. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to invert a word String

    pointers pointers.. please elaborate to me what those pointer do..

    please give me some statements on what a pointer means.. in C and the equivalent of pointers in java

  16. #14
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to invert a word String

    Warning: Pointers are known to confuse the hell out of a lot of people!

    Contributor's Corner

  17. #15
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to invert a word String

    Use function in String class

    public String reverse()

  18. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to invert a word String

    chronoz, you may not want to know what pointers are They are not available in Java, and still confuse me on how and when to use them (granted, it's been quite a while since i've used C/C++)

  19. #17
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to invert a word String

    tnx helloworld sori for being hardheaded i just got curious about the post that says 'use pointers' bla bla
    although some of you says that THERE are no pointers in java and it only exist in C languages i still
    researched a liitle bit about it.. and what ive learned is that pointer is another data type..for some memory alocation for a declared variable... whahahahah anyway tnx... !!