Hi, I've recently started learning java in college. Hoping someone can throw me a bone.
I was give an assignment to make a method to reverse a sentence.
I can only use loops, if statements and other methods(no arrays).
For example "Hello there how are you" would be output as "you are how there Hello"

I've gotten 90% of it working, but it keeps excluding the first word i enter.
So the output that keeps appearing is "you are how there" (the "hello" is missing).

//start backwards method
public static String backwards(String theString)
{
	String newString = "";
	int i = 0;
	int space = 0;
	int lastSpace = theString.length()-1;
	theString = theString.trim();


	for (i = theString.length() - 1; i >= 0; i--)//starting at the end of string
		{

			if (theString.charAt(i) == ' ')
			{
			   space = i;
			   newString = newString + theString.substring(space,lastSpace);
			   lastSpace = i;
			}

		}

		return newString;
	} //end backwards method

Like I said this almost works, except for the very first word entered.
Any ideas? then thanks in advance! =)

I figured it out.
probably the long way around it but....

//start backwards method
	public static String backwards(String theString)
	{
		String newString = "";
		int counter=0;
		int i = 0;
		int space = 0;
		int lastSpace = theString.length() - 1;
		int firstSpace = 0;
		String firstWord = "";
		String oneWord;
		theString = theString.trim();
		
		//FIND THE FIRST WORD
		firstSpace = theString.indexOf(" ");  			//find first space
		//if there are no spaces then it must be one word
		if (firstSpace == -1)
		{
			oneWord = theString;
			newString = oneWord;			//so the newString will return the one word that was entered
		}
		//if there is at least 1 space entered then do the following algorithim to find the backwards sentence
		else
		{
			firstWord = theString.substring(0,firstSpace);	//first word = from position to the first space
			//working^
			
			//for loop starting at the end of string
			for (i = theString.length() - 1; i >= 0; i--)			
			{
				//if to find spaces counting backwards
				if (theString.charAt(i) == ' ')
				{
					space = i;	//space is #i
					newString = newString + theString.substring(space, lastSpace);
					lastSpace = i;	//last space is now #i where variable space was
				}
			}
			//insert the first word at the end of the string
			newString = newString + " " + firstWord;
		}
		
		//return
		return newString;
	}

You see, the forum has such a good effect on you, that you just needed to post the code in order to see how to solve it :)

I'm doing basically the same thing, but I know how to do this way. Me and my teacher are trying to figure out how to write it using only simple string things. Such as string length and a loop. I want it to write a word backwards instead. So instead of "java" it would write "avaj". I've been working on this and it is killing my brain. Please help.. Here is my code..

import java.util.*;
public class backwards
{public static void main(String[]args)
  {String b;
   Scanner q = new Scanner(System.in);
   b = q.nextLine();
   int len, i, j;
   i=0;
   j=0;
   len = b.length();
   System.out.println(len);
   boolean go = true;
   while (i<len)
   {
   System.out.println(b.substring(len - i++));
     String backwards;
     
   }
  
}


}

I have a little more at school, but I am at home at the moment and I need some help. Please and thank you. =D

Member Avatar for ztini
import java.util.Scanner;

public class ReverseSentence {
	public static void main(String[] args) {
		System.out.print("Enter Sentence: ");
		
		String sentence = new Scanner(System.in).nextLine(), backwards = "";
		
		for (int i = 0; i < sentence.length(); i++)
			backwards = sentence.charAt(i) + backwards;
		
		System.out.println("Reversed: " + backwards);
	}
}
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.