how to reverse words in a sentence without using split or string tokenizer.
I/p :- how are you
O/p :- you are how

Since you cannot use split and StringTokenizer, one way is to check one by one of each character from the string if there are any delimiter, put the words inside an array, and then reverse them.

I have already written code which does the same. Just wanted to find a better way to do it.So i could know where I could improve. This aint any school assignment either.

import java.util.ArrayList;


public class Reverser 
{
	public static void main(String args[])
	{
		String myName = "Here we go";
		ArrayList al = new ArrayList();
		al = recursiveMethod(myName,al);
		al.trimToSize();
		StringBuilder sb = new StringBuilder();

		for(int i = al.size()-1; i>=0;i--)
		{
			sb.append(al.get(i)+" ");
			
		}
		System.out.println(sb);
		
	}
	public static ArrayList  recursiveMethod(String myName,ArrayList al)
	{
		
		int index = myName.indexOf(" ");
		al.add(myName.substring(0, index));
		myName  = myName.substring(index+1);
		if(myName.indexOf(" ")==-1)
		{
			al.add(myName.substring(0));
			return al;
		}
		return recursiveMethod(myName,al);
		
	}
}

why make it so complex? you can have the entire functionality from String object to reversed String object in ten lines of code, without having to go recursive.

By using reverse even the letters in the word would get reversed which is not desiered.
requirement is to only reverse the words in the sentence and not the letters :)

I have already written code which does the same. Just wanted to find a better way to do it.So i could know where I could improve. This aint any school assignment either.

OK, thanks for clarifying that. I'm sure you can understand how it looked from your first post.

Anyway, your recursive method is cunning, but could be more understandably coded as a simple loop. You could also add the parsed-out words directly to a result String or StringBuilder without the need for the ArrayList. So I agree with stultuske, this could be a whole lot simpler.

I'm not saying you should use 'reverse', but write a method that does what you need. this can be (as I said) in ten lines of code (or less).

Thanks for your suggestion. Well, i tried removing the Arraylist by taking the lastIndexOf.

import java.util.ArrayList;


public class Reverser 
{
	public static void main(String args[])
	{
		String myName = "Here we go";
		StringBuilder sb = new StringBuilder();
		sb = recursiveMethod(myName,sb);
		System.out.println(sb);
		
	}
	public static StringBuilder  recursiveMethod(String myName,StringBuilder sb)
	{
		
		int index = myName.lastIndexOf(" ");
		sb.append(myName.substring(index));
		myName  = myName.substring(0,index);
		if(myName.indexOf(" ")==-1)
		{
			sb.append(" "+myName.substring(0));
			return sb;
		}
		return recursiveMethod(myName,sb);
		
	}
}

Let me guess - that doesn't reverse the words? As you are picking words from the input you need to insert them at the beginning of the builder, not the end.

The above code works as expected. Was just wondering if there was a way to tweak it a lil bit. :)

I apologise - missed the bit where you use lastIndexOf. Sorry.
I think it's simpler with an ordinary while loop rather than recursion, as in

while ((index = in.indexOf(" ")) >= 0) {
         result = in.substring(0, index) + " " + result;
         in = in.substring(index + 1); 
      }
      return in + " " + result;

I apologise - missed the bit where you use lastIndexOf. Sorry.
I think it's simpler with an ordinary while loop rather than recursion, as in

while ((index = in.indexOf(" ")) >= 0) {
         result = in.substring(0, index) + " " + result;
         in = in.substring(index + 1); 
      }
      return in + " " + result;

Cool.. This seems to be a better approach :).. Thanks !!

No problem. I think combining that loop with you use of lastIndexOf is even cleaner.

I tried that combination. Code turned out to be really short and nice. :)

This question has already been answered. Start a new discussion instead.