reverse the order of the words in a sentence

Discussion in 'Java' started by Matt, Sep 28, 2003.

  1. Matt

    Matt Guest

    Given an array of characters which form a sentence of words, give an
    efficient algorithm to reverse the order of the words (not characters)
    in it.
     
    Matt, Sep 28, 2003
    #1
    1. Advertising

  2. Matt wrote:

    > Given an array of characters which form a sentence of words, give an
    > efficient algorithm to reverse the order of the words (not characters)
    > in it.


    This sounds like homework.

    Hint: look up StringTokenizer and StringBuffer.

    --
    mvg,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Sep 28, 2003
    #2
    1. Advertising

  3. On 28 Sep 2003 11:22:46 -0700, Matt wrote:
    > Given an array of characters which form a sentence of words, give an
    > efficient algorithm to reverse the order of the words (not characters)
    > in it.


    Since this is obviously homework and you haven't shown even the
    slightest attempt at solving it yourself, this concise recursive
    declaration is as much as you'll get from me:

    reverse(word) => word
    reverse(word words) => reverse(words) word

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 28, 2003
    #3
  4. Matt

    Dave Glasser Guest

    Christophe Vanfleteren <> wrote on Sun, 28 Sep
    2003 18:28:37 GMT in comp.lang.java.programmer:

    >Matt wrote:
    >
    >> Given an array of characters which form a sentence of words, give an
    >> efficient algorithm to reverse the order of the words (not characters)
    >> in it.

    >
    >This sounds like homework.


    It's that time of year again.


    --
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net
     
    Dave Glasser, Sep 28, 2003
    #4
  5. Matt

    Roedy Green Guest

    On 28 Sep 2003 11:22:46 -0700, (Matt) wrote or
    quoted :

    >Given an array of characters which form a sentence of words, give an
    >efficient algorithm to reverse the order of the words (not characters)
    >in it.


    see http://mindprod.com/jgloss/homework.html


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 28, 2003
    #5
  6. Matt:

    >Given an array of characters which form a sentence of words, give an
    >efficient algorithm to reverse the order of the words (not characters)
    >in it.


    Why are we supposed to do your homework if you don't even try to put
    some effort in it yourself? Well, maybe you did, but it's not visible.
    <http://www.catb.org/~esr/faqs/smart-questions.html#homework>

    Anyway, here are some hints:

    * Convert character array to a String: String(char[]) constructor.
    * Split a String into words: java.util.StringTokenizer.
    * Put words in a list, e.g. of type ArrayList.
    * Reverse list elements: use java.util.Collections.reverse.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Sep 29, 2003
    #6
  7. Matt

    John Leonard Guest

    I tried to create a program to answer this question. I got this far:

    public class reversetext
    {
    public static void main(Strings[] args)
    {
    int h=0, i=0, j=0, blocklength=0;

    if(args.length) {}
    else {}

    while(i=0; i++; i<blocklength)
    {
    if(block == ('\s' || '\n'))
    {
    endpoints[j] = i;
    j++;
    }
    }
    }
    }

    Why is it that at the line : while(i=0; i++; i<blocklength), I get the
    error: " ')' expected "?

    "Matt" <> wrote in message
    news:...
    > Given an array of characters which form a sentence of words, give an
    > efficient algorithm to reverse the order of the words (not characters)
    > in it.
     
    John Leonard, Sep 29, 2003
    #7
  8. Matt

    Tr0mBoNe- Guest

    Roedy Green <> wrote in message news:<>...
    > On 28 Sep 2003 11:22:46 -0700, (Matt) wrote or
    > quoted :
    >
    > >Given an array of characters which form a sentence of words, give an
    > >efficient algorithm to reverse the order of the words (not characters)
    > >in it.

    >
    > see http://mindprod.com/jgloss/homework.html


    for you C programmers:
    -traverse the array of character (char*)
    -while the character you are currently at is not a space, add it to a
    temporary character array. This means that you have each word in its
    own "special spot" and you can then traverse these with ease.
    -store each temporary char array in another array and then traverse it
    from the end, printing out each value as is. Don't forget to add
    spaces back into this... since we did not store spaces in the first
    place.

    for you JAVA programmers:
    -take in the string.
    -tokenize it with Java.util.StringTokenizer
    -store each value in an array of strings (String[])
    -traverse the array and print each element starting from the back.
    With spaces, as the StringTokenizer does not store spaces.

    I agree with the posts above... this does sound like a homework
    question. Since I am a university student aswell, I went through this
    too... It takes a bit until you can start visualizing the algorithms
    and solutions to the programs. These String manipulation algorithms
    are the meat and butter of learing to program.

    Cheers.
     
    Tr0mBoNe-, Sep 29, 2003
    #8
  9. Matt

    Digital Puer Guest

    Tr0mBoNe- wrote:
    > for you C programmers:
    > -traverse the array of character (char*)
    > -while the character you are currently at is not a space, add it to a
    > temporary character array. This means that you have each word in its
    > own "special spot" and you can then traverse these with ease.
    > -store each temporary char array in another array and then traverse it
    > from the end, printing out each value as is. Don't forget to add
    > spaces back into this... since we did not store spaces in the first
    > place.
    >
    > for you JAVA programmers:
    > -take in the string.
    > -tokenize it with Java.util.StringTokenizer
    > -store each value in an array of strings (String[])
    > -traverse the array and print each element starting from the back.
    > With spaces, as the StringTokenizer does not store spaces.




    There is a simpler, more elegant way of doing this in-place,
    without using any additional arrays. Hint: You need at most one
    char of additional storage.
     
    Digital Puer, Sep 29, 2003
    #9
  10. John Leonard wrote:

    > I tried to create a program to answer this question. I got this far:
    >
    > public class reversetext
    > {
    > public static void main(Strings[] args)
    > {
    > int h=0, i=0, j=0, blocklength=0;
    >
    > if(args.length) {}
    > else {}
    >
    > while(i=0; i++; i<blocklength)
    > {
    > if(block == ('\s' || '\n'))
    > {
    > endpoints[j] = i;
    > j++;
    > }
    > }
    > }
    > }
    >
    > Why is it that at the line : while(i=0; i++; i<blocklength), I get the
    > error: " ')' expected "?
    >

    You need a for statement, not a while (and you've got the order wrong).

    The for statmenent should be for(INITCODE;CONDITION;INCREMENTING)
    so write it as for(i=0;i<blocklength;i++).

    --
    mvg,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Sep 29, 2003
    #10
  11. Matt

    John Leonard Guest

    What an embarrassing mistake. Thank You!

    John

    Christophe Vanfleteren <> wrote in message news:<c8Tdb.47010$-ops.be>...
    > John Leonard wrote:
    >
    > > I tried to create a program to answer this question. I got this far:
    > >
    > > public class reversetext
    > > {
    > > public static void main(Strings[] args)
    > > {
    > > int h=0, i=0, j=0, blocklength=0;
    > >
    > > if(args.length) {}
    > > else {}
    > >
    > > while(i=0; i++; i<blocklength)
    > > {
    > > if(block == ('\s' || '\n'))
    > > {
    > > endpoints[j] = i;
    > > j++;
    > > }
    > > }
    > > }
    > > }
    > >
    > > Why is it that at the line : while(i=0; i++; i<blocklength), I get the
    > > error: " ')' expected "?
    > >

    > You need a for statement, not a while (and you've got the order wrong).
    >
    > The for statmenent should be for(INITCODE;CONDITION;INCREMENTING)
    > so write it as for(i=0;i<blocklength;i++).
     
    John Leonard, Sep 29, 2003
    #11
  12. Matt

    satyam000

    Joined:
    Aug 16, 2010
    Messages:
    1
    in Java you can do like this (One of the method to do ):-

    package kaushal;

    public class ReverseByWord {


    public static void main(String[] args) {

    String str = "this is a beautiful daY";

    System.out.println("Given string-->"+str);

    String arr[] = str.split(" ");

    int i=arr.length-1;
    {
    System.out.println("Given string in 'reverse by words'---->"+arr+" "+arr[i-1]+" "+arr[i-2]+" "+arr[i-3]+" "+arr[i-4]);
    }
    }


    }

    Output->

    Given string-->this is a beautiful daY
    Given string in 'reverse by words'---->daY beautiful a is this
     
    Last edited: Aug 16, 2010
    satyam000, Aug 16, 2010
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    8
    Views:
    1,214
  2. Shobhit

    program to reverse a sentence

    Shobhit, Mar 29, 2006, in forum: C Programming
    Replies:
    3
    Views:
    1,263
    Keith Thompson
    Mar 29, 2006
  3. kirankashyap
    Replies:
    0
    Views:
    927
    kirankashyap
    Aug 28, 2011
  4. mike
    Replies:
    6
    Views:
    194
  5. Raimon Fs
    Replies:
    10
    Views:
    332
    Raimon Fs
    Apr 7, 2009
Loading...

Share This Page