{"id":415,"date":"2013-05-02T17:23:19","date_gmt":"2013-05-03T00:23:19","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=415"},"modified":"2013-05-13T14:49:29","modified_gmt":"2013-05-13T21:49:29","slug":"ctci-ch-1-4","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=415","title":{"rendered":"CTCI: Ch. 1-4"},"content":{"rendered":"<p>\n\t<strong>Write a method to replace all spaces in a string with &lsquo;%20&rsquo;. 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 &ldquo;true&rdquo; length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)<br \/>\n\tEXAMPLE<br \/>\n\tInput: &ldquo;Mr John Smith &rdquo;<br \/>\n\tOutput: &ldquo;Mr%20John%20Smith&rdquo;<\/strong>\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cstring&gt; \/\/ for strlen, but in Xcode, it&#39;s not necessary to include this\r\nusing namespace std;\r\n\r\nvoid replaceSpaces(char originalStr[])\r\n{\r\n    int spaceCount = 0;\r\n    int newLength = 0;\r\n    int length = (int)strlen(originalStr);\r\n    for(int i=0;i&lt;length;i++)\r\n    {\r\n        if(originalStr[i] == &#39; &#39;)\r\n            spaceCount++;\r\n    }\r\n    newLength = length + spaceCount * 2; \/\/not * 3 because original space has been counted once\r\n    originalStr[newLength] = &#39;\\0&#39;; \/\/string terminate\r\n    for(int i = length -1;i&gt;=0;i--)\r\n    {\r\n        if(originalStr[i] == &#39; &#39;)\r\n        {\r\n            originalStr[newLength - 1] = &#39;0&#39;;\r\n            originalStr[newLength - 2] = &#39;2&#39;;\r\n            originalStr[newLength - 3] = &#39;%&#39;;\r\n            newLength-=3;\r\n        }\r\n        else\r\n        {\r\n            originalStr[newLength - 1] = originalStr[i];\r\n            newLength--;\r\n        }\r\n    }\r\n}\r\n\r\nint main(int argc, const char * argv[])\r\n{\r\n    char ch[] = &quot;Mr John Smith&quot;;\r\n    replaceSpaces(ch);\r\n    cout&lt;&lt;ch&lt;&lt;endl;\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a method to replace all spaces in a string with &lsquo;%20&rsquo;. 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 &ldquo;true&rdquo; length &hellip; <a href=\"http:\/\/cywang.no-ip.org\/wordpress\/?p=415\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[19,6],"tags":[],"class_list":["post-415","post","type-post","status-publish","format-standard","hentry","category-chapter-1-arrays-and-strings","category-cracking-the-coding-interview"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=415"}],"version-history":[{"count":6,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":432,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions\/432"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}