{"id":174,"date":"2019-01-04T21:29:13","date_gmt":"2019-01-05T05:29:13","guid":{"rendered":"http:\/\/35.243.195.209\/?p=174"},"modified":"2019-01-04T21:29:42","modified_gmt":"2019-01-05T05:29:42","slug":"c-string-apis","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/01\/04\/c-string-apis\/","title":{"rendered":"Implement Java String APIs in C++"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>It is another post related to String in C++. I will introduce some useful codes which implement APIs in Java String.<\/p>\n<h2>Content<\/h2>\n<ol>\n<li><a href=\"#1\">String.startsWith()<\/a><\/li>\n<li><a href=\"#2\">String.endsWith()<\/a><\/li>\n<li><a href=\"#3\">String.toLowerCase()<\/a><\/li>\n<li><a href=\"#4\">String.toUpperCase()<\/a><\/li>\n<li><a href=\"#5\">String.trim()<\/a><\/li>\n<\/ol>\n<h2>Detials<\/h2>\n<p><a name=\"1\"><\/a><\/p>\n<h3>1. String.startsWith()<\/h3>\n<p>We implement <code>a.startsWith(b)<\/code> of java using C++.<\/p>\n<pre><code class=\"c\">bool starts_with(const string &amp; a, const string &amp; b) {\n    return b == a.substr(0, b.size());\n}\nint main() {\n        cout &lt;&lt; starts_with(\"\", \"\") &lt;&lt; endl; \n        cout &lt;&lt; starts_with(\"\", \"abcd\") &lt;&lt; endl; \n        cout &lt;&lt; starts_with(\"abc\", \"abcd\") &lt;&lt; endl;\n        cout &lt;&lt; starts_with(\"abcd\", \"abcd\") &lt;&lt; endl;\n        cout &lt;&lt; starts_with(\"abcde\", \"abcd\") &lt;&lt; endl;\n        cout &lt;&lt; starts_with(\"abcde\", \"\") &lt;&lt; endl;\n}\n\/*\n1\n0\n0\n1\n1\n1\n*\/\n<\/code><\/pre>\n<p>The complexity of <code>substr<\/code> and <code>==<\/code> should be linear to the size of string b.<br \/>\nNote that in <code>a.substr(pos, len)<\/code>, if pos is greater than the string length, an out_of_range exception is thrown.<\/p>\n<p><a name=\"2\"><\/a><\/p>\n<h3>2. String.endsWith()<\/h3>\n<p>We implement <code>a.endsWith(b)<\/code> of java using C++.<\/p>\n<pre><code class=\"c\">bool ends_with(const string &amp; a, const string &amp; b) {\n    return b.size() &lt;= a.size() &amp;&amp; b == a.substr(a.size() - b.size(), b.size());\n}\nint main() {\n    cout &lt;&lt; ends_with(\"\", \"\") &lt;&lt; endl;\n    cout &lt;&lt; ends_with(\"\", \"abc\") &lt;&lt; endl;\n    cout &lt;&lt; ends_with(\"abcd\", \"abc\") &lt;&lt; endl;\n    cout &lt;&lt; ends_with(\"abcd\", \"abcd\") &lt;&lt; endl;\n    cout &lt;&lt; ends_with(\"eabcd\", \"abcd\") &lt;&lt; endl;\n    cout &lt;&lt; ends_with(\"eabcd\", \"\") &lt;&lt; endl;\n}\n\/*\n1\n0\n0\n1\n1\n1\n*\/\n<\/code><\/pre>\n<p><a name=\"3\"><\/a><\/p>\n<h3>3. String.toLowerCase()<\/h3>\n<p>We have <code>int tolower(int ch)<\/code>.<\/p>\n<pre><code class=\"c\">void to_lower(string &amp; str) {\n        for (auto &amp;&amp; ch : str) {\n                ch = tolower(ch);\n        }\n}\nint main() {\n    string str = \"\";\n    to_lower(str); \/\/ \"\"\n\n    str = \"Hello World!\";\n    to_lower(str); \/\/ hello world!\n}\n<\/code><\/pre>\n<p><a name=\"4\"><\/a><\/p>\n<h3>4. String.toUpperCase()<\/h3>\n<p>We have <code>int tolower(int ch)<\/code>.<\/p>\n<pre><code class=\"c\">void to_upper(string &amp; str) {\n        for (auto &amp;&amp; ch : str) {\n                ch = toupper(ch);\n        }\n}\nint main() {\n    string str = \"\";\n    to_upper(str); \/\/ \"\"\n\n    str = \"Hello World!\";\n    to_upper(str); \/\/ HELLO WORLD!\n}\n<\/code><\/pre>\n<p><a name=\"5\"><\/a><\/p>\n<h3>5. String.trim()<\/h3>\n<pre><code class=\"c\">string trim(const string &amp; str) {\n    if (str.empty()) {\n        return str;\n    }\n    size_t left = 0, right = str.size() - 1;\n    while (left &lt; right &amp;&amp; str[left] == ' ') {\n        left++;\n    }\n    while (left &lt; right &amp;&amp; str[right] == ' ') {\n        right++;\n    }\n    return str.substr(left, right - left + 1);\n}\n\nint main() {\n    string str = \"\";\n    cout &lt;&lt; trim(str) &lt;&lt; endl;\n\n    str = \"   Hello World!\";\n    cout &lt;&lt; trim(str) &lt;&lt; endl;\n\n    str = \"Hello World!   \";\n    cout &lt;&lt; trim(str) &lt;&lt; endl;\n\n    str = \"   Hello World!   \";\n    cout &lt;&lt; trim(str) &lt;&lt; endl;\n}\n\/*\n\"\"\nHello World!\nHello World!   \nHello World!   \n*\/\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary It is another post related to String in C++. I will introduce some useful codes which implement APIs in Java String. Content String.startsWith() String.endsWith() String.toLowerCase() String.toUpperCase() String.trim() Detials 1. String.startsWith() We implement a.startsWith(b) of java using C++. bool starts_with(const string &amp; a, const string &amp; b) { return b == a.substr(0, b.size()); } int&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":4,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}