Results 1 to 7 of 7
Like Tree3Likes
  • 2 Post By Norm
  • 1 Post By eRaaaa

Thread: Reverse words of string.

  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Reverse words of string.

    How to reverse words of a string.
    suppose word is "i am boy", then its output will be "boy am i".

    Please give me coding of this problem..........
    Last edited by Vikash Kumar Singh; 08-05-2011 at 05:28 AM.

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Split by spaces.
    Reverse order of words in the that array.
    Join the array with a space as delimiter.


    Edit:
    As this is Java and not C, and to use unsafe code in Java need to do some hacking, and Arrays.reverse() does not exist, I can only come with two
    [nave] methods to reverse the word array:

    Creating a new array:
    Java Code:
    final int n = oldArray.length;
    final String[] newArray = new String[n];
    for (int i = 0, j = n - 1; i < n; i++, j--)
        newArray[i] = oldArray[j];
    Swapping;
    Java Code:
    final int n = words.length;
    for (int i = 0, m = n / 2, j = n - 1; i < m; i++, j--)
        final String temp = words[i]
        words[i] = words[j]
        words[j] = temp
    Last edited by Hibernate; 08-05-2011 at 05:28 AM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    Please give me coding of this problem
    Yes, that saves the OP the trouble of doing it himself and learning how. Much faster way of getting homework done.
    Have some one else do it.
    There's an expression: No pain, no gain. Also true in programming. If the brain is not exercised properly, little is learned.
    JeffGrigg and Karthikeyan_jf like this.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    There is a Iterator class in the jdk which can solve your problem, but I am sure that your teacher do not want to see something like this:

    Java Code:
    		final String I_AM_BOY = "i am boy";
    		BreakIterator iter = BreakIterator.getWordInstance(Locale.ENGLISH);
    		iter.setText(I_AM_BOY);
    		for (int last = iter.last(), next = iter.previous(); next != BreakIterator.DONE; last = next, next = iter.previous()) {
    		    System.out.print(I_AM_BOY.subSequence(next, last));
    		}
    :D
    Hibernate likes this.

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by eRaaaa View Post
    There is a Iterator class in the jdk which can solve your problem, but I am sure that your teacher do not want to see something like this:

    Java Code:
    		final String I_AM_BOY = "i am boy";
    		BreakIterator iter = BreakIterator.getWordInstance(Locale.ENGLISH);
    		iter.setText(I_AM_BOY);
    		for (int last = iter.last(), next = iter.previous(); next != BreakIterator.DONE; last = next, next = iter.previous()) {
    		    System.out.print(I_AM_BOY.subSequence(next, last));
    		}
    :D

    No he does not, and if he does see it and nothing else in its level, he would suspect something.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  6. #6
    delican is offline Member
    Join Date
    Aug 2011
    Posts
    13
    Rep Power
    0

    Default

    snip - don't spoon feed.
    Last edited by sunde887; 08-07-2011 at 12:27 PM. Reason: Don't spoon feed

  7. #7
    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 split method of the string class will be helpful here. This is a fairly simple exercise and won't take much thinking. Hibernate gave some good advice. The split method will return an array of strings based on the regex passed in(regex can be as simple as " ", which can split on spaces). Once you have this split up array, the rest is easy.

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

Posting Permissions

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