-3
public static void reverse() {
    int x=0;
    char temp = 0;
    String toStrore;
    char checkSpace=' ';
    System.out.println("Enter a line to reverse");
    Scanner sc=new Scanner(System.in);
    String userInput=sc.nextLine();
    char[] charArray=userInput.toCharArray();
    for(int i=charArray.length-1;i>=0;i--){
        char tchar=charArray[i];
        while(tchar==checkSpace){
             x = ++i;
            for(int j=x;j<=charArray.length-1;i++){
                temp=(char) (temp+charArray[j]);
                System.out.print(temp);
            }
        }   
    }
} 

请帮我讲逻辑。

注意:我不想使用任何内置函数,除了长度()。

4

2 回答 2

0

试试这个

import java.util.Arrays;

public class Reverse {
    public static void main(String... args) {
        getReversed("Tom Cat");
    }

    public static void getReversed(String str) {
        char[] arr = str.toCharArray();
        int len = 0;
        for (char ch : arr) {
            if (ch == ' ') {
                len++;
            }
        }
        String[] res = new String[len + 1];
        int start = 0;
        int idx = len;
        while (idx > -1) {
            StringBuilder builder = new StringBuilder();
            for (int i = start; i < arr.length; i++) {
                if (arr[i] != ' ') {
                    builder.append(arr[i]);
                    if (i == arr.length - 1) {
                        res[idx--] = builder.toString();
                    }
                } else {
                    res[idx--] = builder.toString();
                    builder.setLength(0);
                    start = i;
                }
            }
        }
        System.out.println(Arrays.toString(res));
    }
}

更新优化代码

public static void main(String... args) {
    String str = "Cat Tom";
    char[] charArr = str.toCharArray();
    int len = charArr.length;
    ArrayList<String> list = new ArrayList<>();
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < len; i++) {
        char ch = charArr[i];
        if (ch == ' ') {
            list.add(stringBuilder.toString());
            stringBuilder = new StringBuilder();
        } else if (i == len - 1) {
            list.add(stringBuilder.append(ch).toString());
        } else {
            stringBuilder.append(ch);
        }
    }
    Collections.reverse(list);
    System.out.println(list);
}

输出

[Cat, Tom]
于 2018-07-07T22:04:43.063 回答
0

这里有一些东西,但你至少必须使用String.charAt()and String.length()。通过向后搜索来反转字符串,直到找到一个空格。然后它会附加空格之后的内容,直到它到达字符串的末尾或找到另一个空格。最后,第一个单词将被添加到结果中。

public class MyClass {
    public static void main(String args[]) {
        System.out.println(reverse("The quick brown fox jumps over the lazy dog"));
    }

    private static String reverse(String data) {
        String result = "";

        // Build the string in reverse as you find spaces
        for (int i = data.length() - 1; i > -1; i--) {
            if (data.charAt(i) == ' ') {
                int j = i + 1;
                while (j < data.length() && data.charAt(j) != ' ') {
                    result += data.charAt(j++);
                }
                result += " ";
            }
        }

        // Add the first word to the result
        int i = 0;
        while (i < data.length() && data.charAt(i) != ' ') {
            result += data.charAt(i++);
        }

        return result;
    }
}

结果

dog lazy the over jumps fox brown quick The
于 2018-07-07T23:02:26.717 回答