Hi guys, I'm kind of stuck on a problem I was assigned.
Pretty much we're trying to reverse words in a sentence.

As in, input = Hi I am new to Java
to:
Java to new am I Hi

I've tried this

String[]Tyler = new String[Sentence.length()];
	Tyler[0] = Sentence;

	for(int i=0;i<Sentence.length();i++)
	{
		if(Position[i] == -1)
			{
			break;
			}
				else
				{
					Position[i] = Tyler[i].indexOf(" ");
					Tyler[i+1] = Tyler[i].substring(Position[i]+1);
					count = count + 1;
				}
	}
	
	for(int i=0;i<4;i++)
	{
		System.out.println(Tyler[i]);
	}
	
	System.out.print(count);
	*/

And all that would do is simply COUNT the words. I would then have to find each individual word, place it into the array, reverse the array.

That's where I'm having trouble.
I cannot import ANYTHING. Has to be done with just basic EasyReader to read in the input and that's it.
I can use as many arrays as I want.

My idea was to find the first space and then by finding that space, I have my first word.
my first word would be:

Position[0] = Sentence.indexOf(" ");
Sentence.substring(0,Position[0]);

But I'm not sure where to work from there.
I don't really want the full solution, just how to get there.

Dani AI

Generated

Nice work getting a working version, , and thanks to for demonstrating an alternate idea (note: that one reverses characters, not words). A compact, robust pattern that avoids repeated indexOf/substring calls is a single pass that records each word's start and end indices, then prints the words in reverse order. It uses only core String and arrays, handles multiple whitespace characters, and runs in O(n) time.

Example (no imports required):

/* single-pass index-based reversal */
EasyReader in = new EasyReader();
String s = in.readLine();
int n = s.length();
int[] start = new int[n];
int[] end = new int[n];
int wc = 0;
boolean inWord = false;

for (int i = 0; i < n; i++) {
  char c = s.charAt(i);
  if (!Character.isWhitespace(c)) {
    if (!inWord) { start[wc] = i; inWord = true; }
  } else {
    if (inWord) { end[wc] = i; wc++; inWord = false; }
  }
}
if (inWord) { end[wc] = n; wc++; }

for (int i = wc - 1; i >= 0; i--) {
  System.out.print(s.substring(start[i], end[i]));
  if (i > 0) System.out.print(" ");
}
System.out.println();

Why this helps

  • Detects any whitespace (spaces, tabs) via Character.isWhitespace.
  • Avoids creating intermediate string fragments while scanning.
  • Easy to adapt if you must preserve original spacing: store spans and print gaps as well.

Quick troubleshooting

  • Empty or all-whitespace input gives wc == 0; skip printing.
  • If you want a shorter solution and are allowed to use String helpers, trim().split("\\s+") produces a words array you can reverse, but that uses regex and creates more small strings.

Recommended Answers

All 3 Replies

This is my new, working, code for determining how many words in a sentence.
Making an improvement!

class Test {
    public static void main(String[] args) {
	System.out.println("Please enter a sentence. I will then reverse it for you.");
	EasyReader input = new EasyReader();
	String Sentence = input.readLine();
	
	int[]Word;
	Word = new int[Sentence.length()];
	int[]Position;
	Position = new int[Sentence.length()];
	int Count = 1;
	
	for(int i=0;i<Sentence.length();i++)
	{
		Position[i]=Sentence.indexOf(" ");
		Sentence = Sentence.substring(Position[i]+1);
		if(Position[i]== -1)
		{
		}
		else
		{
		System.out.println(Sentence);
		Count = Count +1;
		}
	}
	System.out.println(Count + " words are in this sentence. I will now reverse it.");
}
}

Nvm, found the solution.
Here's what I got, works perfectly.

class Test {
    public static void main(String[] args) {

	System.out.println("Please enter a sentence. I will then reverse it for you.");
	EasyReader input = new EasyReader();
	String Sentence = input.readLine();
	
	String[]Word;
	Word = new String[Sentence.length()];
	int[]Position;
	Position = new int[Sentence.length()];
	int Count = 1;
	Word[0] = Sentence;
	int Check;
	Check = 0;
	for(int i=0;i<Sentence.length();i++)
	{
		Position[i]=Word[i].indexOf(" ");
		Check = Position[i];
		if(Check == -1)
		{
		break;
		}
		else
		{
		Word[i+1] = Word[i].substring(Position[i]+1);
		//System.out.println(Word[i+1]);
		Count = Count +1;
		}
	}
	/*for(int i=0;i<Count;i++)
	{
		System.out.println(Position[i]);
	}
	*/
	System.out.println(Count + " words are in this sentence. I will now reverse it.");
	
	for(int i=0;i<Count-1;i++)
	{
		Word[i] = Word[i].substring(0,Position[i]);
	}
	
	while(Count > 0)
	{
		System.out.print(Word[Count-1] + " ");
		Count = Count -1;
	}
}
}

Hello - nice to see that you solved your code however you could condense this abit and still use no imports. Here's the code:

public static void reverseStringTakeTwo( String line )
	{
		// 'input.split("")' splits the string into an array, therefore each character inhabits a position in the array
		String[] letter = line.split("");
		for( int numChars = letter.length-1; numChars > 0; numChars-- )
		{
			System.out.print(letter[numChars]);
		}
	}

Hope this helps a-bit if even it simply provides another way of doing it.

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.