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

Recommended Answers

All 16 Replies

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.

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in coding your homework for you.

DaniWeb Member Rules include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/forums/faq.php?faq=daniweb_policies
http://www.daniweb.com/forums/announcement8-2.html

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.

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. :)

package com.pack;

public class revword {

public static void main(String[] args) {
String s="i am a boy";
String s1="";
int i=s.length()-1,j=s1.length();
while(i>-1)
{
    while(i>-1 && (s.charAt(i)=' '))
    {
        i--;
    }
    s1+=s.substring(i+1,j)+" ";
    j=i;
    i--;
}
s1=s1.trim();
System.out.println(s1);
}

Is there a reason you revived a thread that hasn't been active in 6 years?
Also: congratulations in managing to write code that won't compile.

(s.charAt(i)=' ')

This won't compile. Can you guess why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.