aspose file tools*
The moose likes Java in General and the fly likes Reverse the words in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reverse the words in a string" Watch "Reverse the words in a string" New topic
Author

Reverse the words in a string

akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
This is my first post here.

I am trying to write a program to reverse the string..
Suppose the user enters " The quick fox."
the output should be "Fox quick the."
I did not to capitalize the first letter yet, but will do so later on. Right now the issue is that this code is doing " the quick quick", which is not correct.

I think that I need to have one more loop right inside the 2nd FOR loop.. but i can't figure out how to do the loop. Please help!

Velu Kasirajah
Greenhorn

Joined: Feb 06, 2010
Posts: 15
You can try


Velu Kasirajah
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
Velu Kasirajah wrote:You can try



i would love to do so but i can't for 2 reasons..

1). I have no idea what that means
2). We are not allowed to use methods like those . we are just allowed to use the following ones:

charAt
compareTo (alphabetical order, case-sensitive)
compareToIgnoreCase (alphabetical order, case-insensitive)
concat (+)
endsWith
equals (case-sensitive)
equalsIgnoreCase (case-insensitive)
indexOf (finds a single character)
indexOf (finds a string)
lastIndexOf (finds a single character)
lastIndexOf (finds a string)
length
startsWith
substring (substring from start, up to but not including end)
substring (substring to end of string)
toLowerCase
toUpperCase
trim (trims spaces from ends of string)


thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

akash negi wrote:


This almost works... It grabs the last word. And then removes it from the sentence. However, it depends on a period being present, which is removed when the last word is removed. You need to...

Modify the code that grabs the last word to not depend on the period (maybe you should remove the period first). Put this code in a loop to repeatedly grab the last word. And then form the new sentence from these words.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
for the purpose of this program, we are to assume that the ending will be with a period, no other punctuations.

and the first letter needs to be capitalized in the final output.


that is why I grabbed the last word manually (outside the loop ). so that I can capitalize it manually later on.
and then I treated the (whole string - last word) as the input and then tried to reverse everything else in the loop.

So far everything works great.. and the 2nd for loop is also good, but to go from last to first, it needs to be a nested loop.

and I have no idea how to achieve that.

thanks..
Suman Poluri
Greenhorn

Joined: Aug 04, 2009
Posts: 7


Once you get your newsentence, you can add a period at the end and replace the first character of the string with an upper case version of it.

You can use the split() method, if you are allowed to, to get the words into a String array and read that array backwards to reverse the string.

Does this help?
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
thanks very much for the reply but I am getting a index out of bound error at the first line after for loop.

We can not use arrays in this..

just loops and the bunch of string functions..


thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Welcome to the Ranch

Suman Poluri, I am afraid it helps little for you simply to give code like that. Pete Stein had it right in the first sentence in this post.
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
Campbell Ritchie wrote:Welcome to the Ranch

Suman Poluri, I am afraid it helps little for you simply to give code like that. Pete Stein had it right in the first sentence in this post.


Thanks Campbell Ritchie! I did not quite understand the code lines given by Suman.. I just wanted to reverse the words in a string without using arrays. Allowed to use just the String methods I mentioned and loops. No split(), no scanner,etc.

Thanks..
Suman Poluri
Greenhorn

Joined: Aug 04, 2009
Posts: 7
Campbell Ritchie wrote:Welcome to the Ranch

Suman Poluri, I am afraid it helps little for you simply to give code like that. Pete Stein had it right in the first sentence in this post.


I guess the temptation was a little too much this time. Appreciate reminding me though.

akash negi wrote:
I did not quite understand the code lines given by Suman..


Now that I confused you enough, I can start back at ground zero

Well, you were doing it almost right until your second 'for' loop. Now if you inspect your code there more closely, you are extracting the same word from the sentence repeatedly. Look at using the other flavor for the substring method to help you extract the other words.

akash negi wrote:
I just wanted to reverse the words in a string without using arrays. Allowed to use just the String methods I mentioned and loops. No split(), no scanner,etc.

Using split() and array was a different approach. Nothing related to the code I already posted.
Prakash Durai
Greenhorn

Joined: Feb 24, 2010
Posts: 2
String word="";
String newsentence="";
sentence=sentence.replace(".", " ");
for (int i=0;i<sentence.length();i++){
char c=sentence.charAt(i);
if (c!=' ')
word+=c;
else{
if ("".equals(newsentence)) c='.';
newsentence=word+c+newsentence;
word="";
}
}

System.out.println(newsentence);


Above will get you "fox quick the." in one loop
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
Look at using the other flavor for the substring method to help you extract the other words.


I think that I am missing a loop over there. But I don't quite understand another "flavor". Is it doable without the 2nd loop?
Prakash Durai
Greenhorn

Joined: Feb 24, 2010
Posts: 2


You are right...just one loop is enough for this program ...hope this clarifies
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

"Pico Soft", please check your private messages for an important administrative matter.

Also, please UseCodeTags when posting code to the forums.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
Pico Soft wrote: String sentence = "The quick fox."; //IO.readLine().toString();
sentence = sentence.trim();
sentence = sentence.toLowerCase();
String word="";
String newsentence="";
sentence=sentence.replace(".", " ");
for (int i=0;i<sentence.length();i++){
char c=sentence.charAt(i);
if (c!=' ')
word+=c;
else{
if ("".equals(newsentence)) c='.';
newsentence=word+c+newsentence;
word="";
}
}

System.out.println(newsentence);

You are right...just one loop is enough for this program ...hope this clarifies





Thanks Pico, but I really did not need full code from start to finish. I was really done with my code. just 1 more loop and it would have been perfect..
This was your first post and I think the P.M. is for giving the code without me trying. Well, then I would ask the "sheriff" to leave Pico this time as he is a newbie who just joined today like me.. and I really worked on the code.. I posted the full code previously..

 
 
subject: Reverse the words in a string