登录
  • #刷题

【Java】关于数组/字符串结束符的疑问……(From CareerCup1_4)

sanguine
1989
5
/*

* Write a method to replace all spaces in a string with'%20'.

You may assume that the string has sufficient space at the end of the string

to hold the additional characters, and that you are given the "true" length of the string.

* (if implementing in Java, please use a character array so that you can perform this operation in place.)

* EXAMPLE

Input: "Mr John Smith "

Output: "Mr%20Dohn%20Smith"

*/

做CareerCup1.4的时候发现一个问题,下面是我的代码:
import java.util.Scanner;[br][/br][br][/br]public class ReplaceSpace1_4 {[br][/br][br][/br]    /*[br][/br][br][/br]     * The Question required to implements this Q in place, which means the parameter of the method[br][/br][br][/br]           should be char[] rather than String, and cannot use another char[] inside the method.[br][/br][br][/br]     * Because the length of the char[] is fixed when initial char[][br][/br][br][/br]           So we initial the char[] have enough space to store char.[br][br][/br]     * 1. count the number of spaces in the input string except the space at the end of char[][br][br][/br]       2. calculate the new length needed after replace the space with '%20'[br][/br][br][/br]       3. Traverse the char[] from the tail(the new length location rather than the whole char[])[br][/br][br][/br]                                    to the front to avoid rewrite[br][/br][br][/br]     * Time Complexity: O(n)[br][/br][br][/br]       Space Complexity: O(1)[br][/br][br][/br]     */[br][/br][br][/br]    private static void ReplaceSpace1(char[] str, int length) {[br][br][/br]        int count = 0;  // count the number of spaces(except the space at the end of char[])[br][br][/br]        int temp = 0;   // temporary count the number of successive spaces[br][/br][br][/br]        // the last temp will count the number of space at the end of char[][br][br][/br]        int num;[br][/br][br][/br]        for (num = 0; num < length; num++) {[br][/br][br][/br]            if (str[num] == ' ') {[br][/br][br][/br]                temp++;[br][/br][br][/br]            } else {[br][/br][br][/br]                count = count + temp;[br][/br][br][/br]                temp = 0;   //spaces here are not at the end of the input string, set the temp to 0[br][/br][br][/br]            }[br][/br][br][/br]        }[br][/br][br][/br]        int oldLength = num - temp;  //length except the spaces at the end of char[][br][br][/br]        int newLength = oldLength + (count << 1);  //calculate the target length of the output char[][br][br][/br]        for (int i = oldLength - 1; i >= 0; i--) {[br][/br][br][/br]            if (str[i] == ' ') {    //changes begin from the char[newLength - 1][br][/br][br][/br]                str[newLength - 1] = '0';[br][/br][br][/br]                str[newLength - 2] = '2';[br][/br][br][/br]                str[newLength - 3] = '%';[br][/br][br][/br]                newLength = newLength - 3;[br][/br][br][/br]            } else {[br][/br][br][/br]                str[--newLength] = str[i];[br][/br][br][/br]            }[br][/br][br][/br]        }[br][/br][br][/br]    }[br][/br][br][/br]    public static void main(String[] args) {[br][br][/br]        Scanner input = new Scanner(System.in);[br][/br][br][/br]        System.out.println("Please enter a string: ");[br][/br][br][/br]        while (input.hasNextLine()) {[br][/br][br][/br]            char[] str = new char[inputStr.length() * 3];[br][/br][br][/br]            for (int i = 0; i < inputStr.length(); i++) {[br][/br][br][/br]                str[i] = inputStr.charAt(i);[br][/br][br][/br]            }[br][/br][br][/br]            ReplaceSpace1(str, inputStr.length());[br][/br][br][/br]            System.out.print("String after replace all the spaces: ");[br][/br][br][/br]            for (int i = 0; str[i] != '\0'; i++) {[br][/br][br][/br]                System.out.print(str[i]);[br][/br][br][/br]            }[br][/br][br][/br]            System.out.println();[br][/br][br][/br]            System.out.println("Please enter another string: ");[br][/br][br][/br]        }[br][/br][br][/br]    }[br][/br][br][/br]}有个感觉自己绕不清楚的问题:[br][/br][br][/br]查了很多资料,知道Java是没有数组/字符串结束符的,自己也写代码试了下,发现确实如此,在Java中新建一个char[1000]的数组,并只往前面10个添加数据,然后length还是1000,(在C/C++中英文结束符是\0,所以会自动判断到length是2【求证实】)[br][/br][br][/br]这样的话上面这个代码感觉无解了==[br][/br][br][/br]首先函数中传入了char[]和字符串的长度,然后因为字符串由空格变成了‘%20’,字符串的长度是增加了的,即代码里的newLength长度,但是因为Java里面没有指针,所以newLength没法返回给main函数,所以需要在void main里面自己判断replace后的数组有多长……但是又因为Java没有结束符==没法判断![br][br][/br]现在的写法是[backcolor=rgb(247, 247, 247)]for (int i = 0; str[i] != '\0'; i++)[/i][/backcolor][i][backcolor=rgb(247, 247, 247)] ,但是万一字符串中本身就有\0呢?还是说不要考虑这种情况%>_<%[/backcolor][br][/br][br][/br][/i][i][backcolor=rgb(247, 247, 247)][br][/br][br][/br][/backcolor][/i][br][/br][br][/br][i][backcolor=rgb(247, 247, 247)]---------------------------------------------------------------------update--------------------------------------------------------------------------[/backcolor][/i][br][/br][br][/br]测试了下,输入的是“aaa\0  bb    ”,输出的时候也是正常的[br][/br][br][/br]这是因为我在void main函数中[br][/br][br][/br]用的是[br][/br][br][/br]char[] str = new char[inputStr.length() * 3];[br][/br][br][/br]            for (int i = 0; i < inputStr.length(); i++) {[br][/br][br][/br]                str = inputStr.charAt(i);[br][/br][br][/br]            }[br][/br][br][/br]实现的把string类型放到char[]中,如果使用str = inputStr.tocharArray方法的话,\0就会作为一个整体存入char[]中,这样的话,输出的时候就会直接输出到aaa截止,后面的并不会输出[/i][/i][/i][/i][/i]
5条回复
热度排序

发表回复