Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > reverse the order of the words in a sentence

Reply
Thread Tools

reverse the order of the words in a sentence

 
 
Matt
Guest
Posts: n/a
 
      09-28-2003
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.
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      09-28-2003
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
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      09-28-2003
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
 
Reply With Quote
 
Dave Glasser
Guest
Posts: n/a
 
      09-28-2003
Christophe Vanfleteren <(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-28-2003
On 28 Sep 2003 11:22:46 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
Marco Schmidt
Guest
Posts: n/a
 
      09-29-2003
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
 
Reply With Quote
 
John Leonard
Guest
Posts: n/a
 
      09-29-2003
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[i] == ('\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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.



 
Reply With Quote
 
Tr0mBoNe-
Guest
Posts: n/a
 
      09-29-2003
Roedy Green <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 28 Sep 2003 11:22:46 -0700, (E-Mail Removed) (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.
 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      09-29-2003
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.
 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      09-29-2003
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[i] == ('\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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse the words in a sentence as per length as ajax request kirankashyap XML 0 08-28-2011 12:12 PM
regex select multiple words in the middle of a sentence Raimon Fs Ruby 10 04-07-2009 09:12 PM
how do I parse the words of a sentence? mike ASP General 6 09-28-2006 03:26 AM
program to reverse a sentence Shobhit C Programming 3 03-29-2006 07:23 AM
reverse the order of the words Matt C++ 8 09-29-2003 06:50 PM



Advertisments