Return string entered as reverse text in Java

I am trying to make a method that returns a string of words in the opposite order.

IE / "Rain in Spain falls mainly on" will return: "Spain mainly falls in the rain"

For this, I should not use any Java built-in classes, only basic Java.

So far, I:

    lastSpace = stringIn.length(); 

    for (int i = stringIn.length() - 1; i >= 0; i--){
        chIn = stringIn.charAt(i);
        if (chIn == ' '){
            word = stringIn.substring(i + 1, lastSpace);
            stringOut.concat(word);
            lastS = i;
        }
    }
    word = stringIn.substring(0,lastSpace);
    stringOut.concat(word);

    return stringOut;

My problem is that when returning stringOutit to the caller there will always be an empty string.

Am I doing something wrong? Perhaps my use string.concat()?

+5
source share
6 answers

Java , .. . concat() . , - :

stringOut = stringOut.concat(word);

stringOut += word

, , .

+8
public String reverseWords(String words)
{
  if(words == null || words.isEmpty() || !words.contains(" "))
    return words;

  String reversed = "";
  for(String word : words.split(" "))
    reversed = word + " " + reversed;

  return reversed.trim();
}

API - String ( ...)

+2

, indexOf String, , .

+1
public String reverseString(String originalString)
     {
     String reverseString="";
     String substring[]=originalString.split(" ");// at least one space between this double                      //quotes

    for(int i=(substring.length-1);i>=0;i--)
        {
        reverseString = reverseString + substring[i];
        }

      return sreverseString;
     }
+1

, concat:

stringOut=stringOut.concat(word)

Java ( .net) .

0

, :


import java.util.*;

class ReverseBuffer {
    private StringBuilder soFar;
    public ReverseBuffer() {
        soFar = new StringBuilder();
    }

    public void add(char ch) {
        soFar.append(ch);
    }

    public String getReversedString() {
        String str = soFar.toString();
        soFar.setLength(0);
        return str;
    }
}

public class Reverso {
    public static String[] getReversedWords(String sentence) {
        ArrayList &lt String &gt strings = new ArrayList &lt String &gt();
        ReverseBuffer rb = new ReverseBuffer();
        for(int i = 0;i &lt sentence.length();i++) {
            char current = sentence.charAt(i);
            if(current == ' ') {
                strings.add(rb.getReversedString());
            }
            else {
                rb.add(current);
            }
        }
        strings.add(rb.getReversedString());
        Collections.reverse(strings);
        return (String[])strings.toArray(new String[0]);
    }

    public static void main(String[] args) {
        String cSentence = "The rain in Spain falls mostly on the";
        String words[] = Reverso.getReversedWords(cSentence);
        for(String word : words) {
            System.out.println(word);
        }
    }
}

EDIT: getReversedString .

, !

-2

All Articles