在不使用内置函数的情况下逐字反转字符串

时间:2015-09-25 13:36:09

标签: java string

我写了一个程序,无法逐字逐句地表达字符串。

输入:

  

这是个男孩

预期产出:

  

男孩是这个

收到的输出:

  男孩男孩是男孩

你能帮我完成/更正代码吗?

public class Title {
    public static void main(String args[]) {
        String rev = "This is a boy";
        char a[] = new char[rev.length()];
        int i, j;
        for (i = a.length - 1; i >= 0; i--) {
            a[i] = rev.charAt(i);
            if (a[i] == ' ' || i == ' ') {
                for (j = i + 1; j < a.length; j++) {
                    a[j] = rev.charAt(j);
                }
                String n = new String(a);
                System.out.print(n);
            }
        }
    }
}

4 个答案:

答案 0 :(得分:3)

将功能分离到较低级别的功能然后使用它们总是很好的方法。它更易读,更容易测试和修复。

在您的情况下,可以这样做

public static void main(String[] args) {
    String input = "This is a boy";
    List<String> separated = separateWords(input);
    for (int i = separated.size() - 1; i >= 0; i--) {
        System.out.print(separated.get(i));
        if (i != 0){
            System.out.print(" ");
        }
    }
}   

public static List<String> separateWords(String line) {
    List<String> list = new ArrayList<String>();
    String actualString = "";
    for (int i = 0; i < line.length(); i++) {
        if ((line.charAt(i) == ' ') || (i == line.length() - 1)) {
            if (i == line.length() - 1){
                actualString += line.charAt(i);
            }
            list.add(actualString);
            actualString = "";
        } else {
            actualString += line.charAt(i);
        }
    }
    return list;
}

如果你坚持&#34;你的&#34;解决方案,你最不重要的是&#34;结束&#34;。就我而言,它是变量&#34; max&#34;。它会记住您要复制的单词的结尾。

public static void main(String[] args) {
    String rev = "This is a boy";
    String out = "";

    int skipSpace = 1;
    int max = rev.length();
    for (int i = rev.length() - 1; i >= 0; i--) {
        if (rev.charAt(i) == ' ' || i == 0) {                
            if (i==0){
                skipSpace = 0;
            }
            for (int j = i + skipSpace; j < max; j++) {
                out += rev.charAt(j);
            }
            if (i != 0) {
                out += " ";
            }
            max = i;
        }
    }
    System.out.println(out);
}

答案 1 :(得分:3)

经过测试和确认工作,并且它不使用除length()charAt(int)之外的任何字符串函数(两者都是您使用的,所以我认为它没问题):

public static void main(String []args){
    String rev = "This is a boy";
    String currentWord = "";

    for (int i=rev.length()-1; i>=0; i--)
    {
        if (rev.charAt(i) == ' ')
        {
            printWordReversed(currentWord);
            currentWord = "";
        }
        else
            currentWord = currentWord + rev.charAt(i);
    }

    // Have to print out the last word
    printWordReversed(currentWord);
    System.out.println(); // new line for formatting
 }

 public static void printWordReversed(String word)
 {
     for(int i=word.length()-1; i>=0; i--)
     {
         System.out.print(word.charAt(i));
     }
     System.out.print(" ");
 }

Java fiddle

这种方式的工作方式是存储当前的&#34;字&#34;在字符串中循环遍历字符串中的每个字符。只要你碰到一个空格,就打印并清除当前的单词。但是,由于我们向后遍历字符串,我们提取的单词将以相反的顺序排列,因此我们必须重新对它们进行反转。

答案 2 :(得分:2)

在这里,

  1. 方法:

    检查是否有空格,如果有,则将char复制到其他数组。第一个单词的最后一个,使用其他部分复制它,因为第一个字符没有空格:

    public class Title {
        public static void main(String args[]) {
            String rev = "This is a boy";
            char a[] = new char[rev.length()];
            int i, j;
            int index =0;
            int previous = a.length;
            for (i = a.length - 1; i >= 0; i--) {
                if (rev.charAt(i) == ' ' ) {
                    for (j = i + 1; j < previous; j++) {
                        a[index] = rev.charAt(j);
                        index ++;
                    }
                    a[index++] = ' ';
                    previous = i;
                }
            }
            for (j = 0; j < previous; j++) {
                a[index] = rev.charAt(j);
                index ++;
            }
            String n = new String(a);
            System.out.print(n);
        }
    }
    
  2. 方法:

    首先拆分字符串然后以相反的顺序打印:

    public class Title {
        public static void main(String args[]) {
            String rev = "This is a boy";
            String []a = rev.split(" ");
    
            for (int i = a.length - 1; i >= 0; i--) {
                if (i == a.length-1)
                    System.out.print(a[i]);
                else
                    System.out.print(" "+a[i]);
            }
        }
    }
    

答案 3 :(得分:2)

当你遇到“C:\Program Files\SmartBear\SoapUI-5.2.0\bin\testrunner.bat" DemoPing.xml 时,从后面阅读这些单词并将它们存储在单独的String中。将单词添加到whitespace后,单独添加whitespace

reverseString
相关问题