reverse the order of the words in a sentence

M

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.
 
C

Christophe Vanfleteren

Matt said:
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.
 
G

Gordon Beaton

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
 
M

Marco Schmidt

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
 
J

John Leonard

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 "?
 
T

Tr0mBoNe-

Roedy Green said:

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.
 
D

Digital Puer

Tr0mBoNe- said:
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.
 
C

Christophe Vanfleteren

John said:
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++).
 
J

John Leonard

What an embarrassing mistake. Thank You!

John

Christophe Vanfleteren said:
John said:
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++).
 
Joined
Aug 16, 2010
Messages
1
Reaction score
0
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:

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top