{"id":23966,"date":"2022-11-29T11:19:34","date_gmt":"2022-11-29T05:49:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/"},"modified":"2022-11-29T11:19:34","modified_gmt":"2022-11-29T05:49:34","slug":"solved-string-functions-strcat","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/","title":{"rendered":"[Solved] String Functions: Strcat()"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-57849347\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"57849347\" data-parentid=\"57848625\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You have a number of not just quite right beginning to each of your functions. Firstly, let&#8217;s think about the returns for each. <code>myStrlen<\/code> should return <code>size_t<\/code> instead of <code>int<\/code>. C++ designates a size_type for counters, measuring, etc.. The remaining functions should return <code>char*<\/code> (or <code>nullptr<\/code> on failure).<\/p>\n<p>Looking at your <code>myStrlen<\/code> function where you have<\/p>\n<pre><code>for (i=0; str1[i] != '\\0'; i++)\nstr1[i] = '\\0';\n<\/code><\/pre>\n<p>You are setting every character in <code>str1<\/code> to the <em>nul-character<\/em> because you are applying the loop to the next expression. You should not be worrying about nul-terminating anything within <code>myStrlen<\/code> &#8212; you are just counting characters. So you can rewrite it as follows:<\/p>\n<pre><code>size_t myStrlen (const char *str)\n{\n    size_t l = 0;\n    for (; str[l]; l++) {}\n    return l;\n}\n<\/code><\/pre>\n<p>Your <code>myStrcpy<\/code> looks workable, though you should always validate your input parameters are not <code>nullptr<\/code> before using them &#8212; I leave that to you. Since you have a <code>myStrlen<\/code> function, you can simply use that along with <code>memcpy<\/code> to create your <code>myStrcpy<\/code> function as:<\/p>\n<pre><code>char *myStrcpy (char *dest, const char *src)\n{\n    size_t len = myStrlen(src);\n    return (char *)memcpy (dest, src, len + 1);\n}\n<\/code><\/pre>\n<p>(<strong>note:<\/strong> traditionally you have source (<code>src<\/code>) and destination (<code>dest<\/code>) parameters when copying or concatenating)<\/p>\n<p>For your <code>myStrcat<\/code> function, you are just using the <code>myStrlen<\/code> function to find the offset in <code>dest<\/code> to append <code>src<\/code>, so you really just need a call to <code>myStrlen<\/code> and then a call to <code>myStrcpy<\/code> to copy <code>src<\/code> to that offset in <code>dest<\/code>, e.g.<\/p>\n<pre><code>char *myStrcat (char *dest, const char *src)\n{\n    size_t len = myStrlen (dest);\n    return myStrcpy (dest + len, src);\n}\n<\/code><\/pre>\n<p>In your <code>main()<\/code>, if you want a space between <code>\"Hello\"<\/code> and <code>\"World\"<\/code>, then <code>const int SIZE = 11;<\/code> is one too-low to hold the concatenated string <code>\"Hello World\"<\/code> which would require <code>12-bytes<\/code> (including the <em>nul-terminating<\/em> character). <em>Do Not Skimp<\/em> on buffer size. <code>128<\/code> is plenty small.<\/p>\n<p>Remaining with your <code>main()<\/code> but updating <code>SIZE = 12;<\/code> and adding a space between <code>\"Hello\"<\/code> and <code>\"World\"<\/code> with an additional call to <code>myStrcat<\/code>, you could do the following:<\/p>\n<pre><code>int main (void)\n{\n    const int SIZE = 12;    \/* too short by 1 if you add space between *\/\n    char s1[SIZE] = \"Hello\";\n    char s2[SIZE] = \"World\";\n\n    std::cout &lt;&lt; \"s1: \" &lt;&lt; \" \" &lt;&lt; s1 &lt;&lt; std::endl &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"The length of s1: \" &lt;&lt; myStrlen(s1) &lt;&lt; std::endl &lt;&lt; std::endl;\n\n    std::cout &lt;&lt; \"Doing strcat(s1, s2) \" &lt;&lt; std::endl;\n    myStrcat(s1, \" \");\n    myStrcat(s1, s2);\n    std::cout &lt;&lt; \"s1: \" &lt;&lt; \" \" &lt;&lt; s1 &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"The length of s1: \" &lt;&lt; myStrlen(s1) &lt;&lt; std::endl &lt;&lt; std::endl;\n\n    std::cout &lt;&lt; \"Doing strcpy(s1, s2) \" &lt;&lt; std::endl;\n    myStrcpy(s1, s2);\n    std::cout &lt;&lt; \"s1: \" &lt;&lt; \" \" &lt;&lt; s1 &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"The length of s1: \" &lt;&lt; myStrlen(s1) &lt;&lt; std::endl &lt;&lt; std::endl;\n}\n<\/code><\/pre>\n<p>(<strong>note:<\/strong> don&#8217;t include <code>using namespace std;<\/code>, it is just bad form in this day and age)<\/p>\n<p><strong>Example Use\/Output<\/strong><\/p>\n<pre><code>$.\/bin\/mystrcpy\ns1:  Hello\n\nThe length of s1: 5\n\nDoing strcat(s1, s2)\ns1:  Hello World\nThe length of s1: 11\n\nDoing strcpy(s1, s2)\ns1:  World\nThe length of s1: 5\n<\/code><\/pre>\n<p>Look things over and let me know if you have further questions.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved String Functions: Strcat() <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You have a number of not just quite right beginning to each of your functions. Firstly, let&#8217;s think about the returns for each. myStrlen should return size_t instead of int. C++ designates a size_type for counters, measuring, etc.. The remaining functions should return char* (or nullptr on failure). Looking at your myStrlen function where &#8230; <a title=\"[Solved] String Functions: Strcat()\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\" aria-label=\"More on [Solved] String Functions: Strcat()\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-23966","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] String Functions: Strcat() - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] String Functions: Strcat() - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You have a number of not just quite right beginning to each of your functions. Firstly, let&#8217;s think about the returns for each. myStrlen should return size_t instead of int. C++ designates a size_type for counters, measuring, etc.. The remaining functions should return char* (or nullptr on failure). Looking at your myStrlen function where ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-29T05:49:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] String Functions: Strcat()\",\"datePublished\":\"2022-11-29T05:49:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\"},\"wordCount\":275,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\",\"name\":\"[Solved] String Functions: Strcat() - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-29T05:49:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] String Functions: Strcat()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] String Functions: Strcat() - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] String Functions: Strcat() - JassWeb","og_description":"[ad_1] You have a number of not just quite right beginning to each of your functions. Firstly, let&#8217;s think about the returns for each. myStrlen should return size_t instead of int. C++ designates a size_type for counters, measuring, etc.. The remaining functions should return char* (or nullptr on failure). Looking at your myStrlen function where ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/","og_site_name":"JassWeb","article_published_time":"2022-11-29T05:49:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] String Functions: Strcat()","datePublished":"2022-11-29T05:49:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/"},"wordCount":275,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/","url":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/","name":"[Solved] String Functions: Strcat() - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-29T05:49:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-string-functions-strcat\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] String Functions: Strcat()"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/23966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=23966"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/23966\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=23966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=23966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=23966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}