Recently in the brush "sword refers to offer" in the programming problem, but the internet about "sword refers to offer" solution is mostly C or C + +, and the official (author) is also in C + + to explain, here himself with Java wrote some of the answer code of the topic (of course, there are also some of the answers to the online others, The source does not indicate please point out, the invasion of the deletion, hope to be able to help everyone's study.
Topic Description Please implement a function that replaces a space in a string with a "%20". For example, when the string is Are Happy, the replaced string is We%20are%20happy. New Ket Network Topic Screenshot:
PS. This requires that the incoming parameter be stringbuffer type .
Answer:
[Java] View Plain Copy publicclasssolution { public String Replacespace (STRINGBUFFER&NBSP;STR) { for (int k=0; k<str.length () k++) { char index = str.charat (k); if (index == ' ') { str.replace (k, k+1, "%20"); //here with the ingenious, do not considerAfter the transformation length problem. } } return str.tostring (); } }