Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reverse the words in a string

 
akash negi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try
 
akash negi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20893
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
akash negi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are right...just one loop is enough for this program ...hope this clarifies
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Pico Soft", please check your private messages for an important administrative matter.

Also, please UseCodeTags when posting code to the forums.
 
akash negi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic