{"id":14625,"date":"2024-05-04T22:00:32","date_gmt":"2024-05-04T19:00:32","guid":{"rendered":"https:\/\/elzero.org\/?p=14625"},"modified":"2024-05-04T22:15:31","modified_gmt":"2024-05-04T19:15:31","slug":"implement-functions-with-cpp-trim-string","status":"publish","type":"post","link":"https:\/\/elzero.org\/old\/implement-functions-with-cpp-trim-string\/","title":{"rendered":"Trim String"},"content":{"rendered":"<h3>\u0645\u062d\u062a\u0648\u0649 \u0627\u0644\u062f\u0631\u0633<\/h3>\r\n\r\n<script type=\"text\/plain\" class=\"language-c\">\r\n#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\n\/\/ Trim [Left|Right] Characters\r\n\r\n\/\/ ###Elzero### -> All -> Elzero\r\n\/\/ ###Elzero### -> Left -> Elzero###\r\n\/\/ ###Elzero### -> Right -> ###Elzero\r\n\r\nstring trim_string(string st, string direction = \"All\", char ch = ' ')\r\n{\r\n  string result;\r\n  int firstCharsCount = 0;\r\n  int lastCharsCount = 0;\r\n\r\n  for (int i = 0; i < st.length(); i++)\r\n  {\r\n    if (st[i] == ch)\r\n    {\r\n      firstCharsCount++;\r\n    }\r\n    else\r\n    {\r\n      break;\r\n    }\r\n  }\r\n\r\n  for (int i = st.length() - 1; i >= 0; i--)\r\n  {\r\n    if (st[i] == ch)\r\n    {\r\n      lastCharsCount++;\r\n    }\r\n    else\r\n    {\r\n      break;\r\n    }\r\n  }\r\n\r\n  if (direction == \"Left\")\r\n  {\r\n    for (int i = firstCharsCount; i < st.length(); i++)\r\n    {\r\n      result += st[i];\r\n    }\r\n  }\r\n  else if (direction == \"Right\")\r\n  {\r\n    for (int i = 0; i < st.length() - lastCharsCount; i++)\r\n    {\r\n      result += st[i];\r\n    }\r\n  }\r\n  else if (direction == \"All\")\r\n  {\r\n    for (int i = firstCharsCount; i < st.length() - lastCharsCount; i++)\r\n    {\r\n      result += st[i];\r\n    }\r\n  }\r\n\r\n  \/\/ cout << firstCharsCount << endl; \/\/ 5\r\n  \/\/ cout << lastCharsCount << endl;\r\n\r\n  return result;\r\n}\r\n\r\nint main()\r\n{\r\n  cout << trim_string(\"#####Elzero##\", \"Right\", '#') << endl;\r\n  return 0;\r\n}\r\n<\/script>","protected":false},"excerpt":{"rendered":"<p>\u0645\u062d\u062a\u0648\u0649 \u0627\u0644\u062f\u0631\u0633<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1454],"tags":[],"class_list":["post-14625","post","type-post","status-publish","format-standard","hentry","category-implement-functions-with-cpp"],"_links":{"self":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/14625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/comments?post=14625"}],"version-history":[{"count":1,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/14625\/revisions"}],"predecessor-version":[{"id":14626,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/14625\/revisions\/14626"}],"wp:attachment":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/media?parent=14625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/categories?post=14625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/tags?post=14625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}