{"id":19903,"date":"2024-03-15T19:37:01","date_gmt":"2024-03-15T19:37:01","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/"},"modified":"2024-03-21T16:48:41","modified_gmt":"2024-03-21T16:48:41","slug":"how-to-use-recursion-in-c-language-to-count-the-number-of-words","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/","title":{"rendered":"How to use recursion in C language to count the number of words"},"content":{"rendered":"<p>Here is an example code in C language that calculates the number of words using recursion.<\/p>\n<pre class=\"post-pre\">#include&nbsp;&lt;stdio.h&gt;<p><\/p><p>\/\/&nbsp;\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570<\/p><p>int&nbsp;countWords(char&nbsp;str[])&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;wordCount&nbsp;=&nbsp;0;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;isWord&nbsp;=&nbsp;0;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;'\\0')&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;'&nbsp;'&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;'\\n'&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;'\\t')&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;0;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(isWord&nbsp;==&nbsp;0)&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;1;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wordCount++;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;countWords(str&nbsp;+&nbsp;1);&nbsp;\/\/&nbsp;\u9012\u5f52\u8c03\u7528\uff0c\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;<\/p><p>}<\/p><p>int&nbsp;main()&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;str[]&nbsp;=&nbsp;\"Hello&nbsp;World,&nbsp;this&nbsp;is&nbsp;a&nbsp;sentence.\";<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8c03\u7528\u9012\u5f52\u51fd\u6570\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numWords&nbsp;=&nbsp;countWords(str);<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;printf(\"\u5355\u8bcd\u4e2a\u6570\uff1a%d\\n\",&nbsp;numWords);<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;<\/p><p>}<\/p><\/pre>\n<p>In the above code, we have defined a recursive function called countWords to calculate the number of words. In each recursive call, we check if the current character is a space, line break, or tab character. If it is, we set isWord to 0. Otherwise, if isWord is 0, it means we have encountered a new word. We then set it to 1 and increase the word counter wordCount.<\/p>\n<p>In the main function, we defined an example string str and then called the countWords function to calculate the number of words and print the result.<\/p>\n<p>After running the code, the output will be: Number of words: 6 (based on the example string). Make sure to replace the example string with the actual string you want to count the number of words for when using it.<\/p>\n<p>Below is an example code in C language using recursion to count the number of words.<\/p>\n<pre class=\"post-pre\">#include&nbsp;&lt;stdio.h&gt;<p><\/p><p>\/\/&nbsp;\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570<\/p><p>int&nbsp;countWords(char&nbsp;str[])&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;wordCount&nbsp;=&nbsp;0;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;isWord&nbsp;=&nbsp;0;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;'\\0')&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;'&nbsp;'&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;'\\n'&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;'\\t')&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;0;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(isWord&nbsp;==&nbsp;0)&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;1;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wordCount++;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;countWords(str&nbsp;+&nbsp;1);&nbsp;\/\/&nbsp;\u9012\u5f52\u8c03\u7528\uff0c\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;<\/p><p>}<\/p><p>int&nbsp;main()&nbsp;{<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;str[]&nbsp;=&nbsp;\"Hello&nbsp;World,&nbsp;this&nbsp;is&nbsp;a&nbsp;sentence.\";<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8c03\u7528\u9012\u5f52\u51fd\u6570\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numWords&nbsp;=&nbsp;countWords(str);<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;printf(\"\u5355\u8bcd\u4e2a\u6570\uff1a%d\\n\",&nbsp;numWords);<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;<\/p><p>}<\/p><\/pre>\n<p>In the above code, we have defined a recursive function countWords to count the number of words. In each recursive call, we check if the current character is a space, newline, or tab. If it is, we set isWord to 0. Otherwise, if isWord is 0, it means we have encountered a new word, so we set it to 1 and increase the word counter wordCount.<\/p>\n<p>In the main function, we defined an example string str, then called the countWords function to calculate the number of words, and printed out the result.<\/p>\n<p>By running the code, it will output the result as: Number of words: 6 (based on the example string). Make sure to replace the example string with the actual string you want to count the number of words for when using it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example code in C language that calculates the number of words using recursion. #include&nbsp;&lt;stdio.h&gt;\/\/&nbsp;\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570int&nbsp;countWords(char&nbsp;str[])&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;wordCount&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;isWord&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;&#8216;\\0&#8217;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;&#8216;&nbsp;&#8216;&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;&#8216;\\n&#8217;&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;&#8216;\\t&#8217;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(isWord&nbsp;==&nbsp;0)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wordCount++;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;countWords(str&nbsp;+&nbsp;1);&nbsp;\/\/&nbsp;\u9012\u5f52\u8c03\u7528\uff0c\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;}int&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;str[]&nbsp;=&nbsp;&#8220;Hello&nbsp;World,&nbsp;this&nbsp;is&nbsp;a&nbsp;sentence.&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8c03\u7528\u9012\u5f52\u51fd\u6570\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numWords&nbsp;=&nbsp;countWords(str);&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;\u5355\u8bcd\u4e2a\u6570\uff1a%d\\n&#8221;,&nbsp;numWords);&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;} In the above code, we have defined a recursive function called countWords to calculate the number of words. In each recursive call, we check if the current character is a space, line break, or tab character. If it [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-19903","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use recursion in C language to count the number of words - Blog - Silicon Cloud<\/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:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use recursion in C language to count the number of words\" \/>\n<meta property=\"og:description\" content=\"Here is an example code in C language that calculates the number of words using recursion. #include&nbsp;&lt;stdio.h&gt;\/\/&nbsp;\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570int&nbsp;countWords(char&nbsp;str[])&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;wordCount&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;isWord&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;&#039;&#039;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;&#039;&nbsp;&#039;&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;&#039;n&#039;&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;&#039;t&#039;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(isWord&nbsp;==&nbsp;0)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wordCount++;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;countWords(str&nbsp;+&nbsp;1);&nbsp;\/\/&nbsp;\u9012\u5f52\u8c03\u7528\uff0c\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;}int&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;str[]&nbsp;=&nbsp;&quot;Hello&nbsp;World,&nbsp;this&nbsp;is&nbsp;a&nbsp;sentence.&quot;;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8c03\u7528\u9012\u5f52\u51fd\u6570\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numWords&nbsp;=&nbsp;countWords(str);&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\u5355\u8bcd\u4e2a\u6570\uff1a%dn&quot;,&nbsp;numWords);&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;} In the above code, we have defined a recursive function called countWords to calculate the number of words. In each recursive call, we check if the current character is a space, line break, or tab character. If it [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T19:37:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T16:48:41+00:00\" \/>\n<meta name=\"author\" content=\"Noah Thompson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Noah Thompson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\"},\"author\":{\"name\":\"Noah Thompson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\"},\"headline\":\"How to use recursion in C language to count the number of words\",\"datePublished\":\"2024-03-15T19:37:01+00:00\",\"dateModified\":\"2024-03-21T16:48:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\"},\"wordCount\":306,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\",\"name\":\"How to use recursion in C language to count the number of words - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T19:37:01+00:00\",\"dateModified\":\"2024-03-21T16:48:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use recursion in C language to count the number of words\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\",\"name\":\"Noah Thompson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"caption\":\"Noah Thompson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use recursion in C language to count the number of words - Blog - Silicon Cloud","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:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/","og_locale":"en_US","og_type":"article","og_title":"How to use recursion in C language to count the number of words","og_description":"Here is an example code in C language that calculates the number of words using recursion. #include&nbsp;&lt;stdio.h&gt;\/\/&nbsp;\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\u6765\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570int&nbsp;countWords(char&nbsp;str[])&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;wordCount&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;isWord&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;'')&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(str[0]&nbsp;==&nbsp;'&nbsp;'&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;'n'&nbsp;||&nbsp;str[0]&nbsp;==&nbsp;'t')&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(isWord&nbsp;==&nbsp;0)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isWord&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wordCount++;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;countWords(str&nbsp;+&nbsp;1);&nbsp;\/\/&nbsp;\u9012\u5f52\u8c03\u7528\uff0c\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wordCount;}int&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;str[]&nbsp;=&nbsp;\"Hello&nbsp;World,&nbsp;this&nbsp;is&nbsp;a&nbsp;sentence.\";&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8c03\u7528\u9012\u5f52\u51fd\u6570\u8ba1\u7b97\u5355\u8bcd\u4e2a\u6570&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numWords&nbsp;=&nbsp;countWords(str);&nbsp;&nbsp;&nbsp;&nbsp;printf(\"\u5355\u8bcd\u4e2a\u6570\uff1a%dn\",&nbsp;numWords);&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;} In the above code, we have defined a recursive function called countWords to calculate the number of words. In each recursive call, we check if the current character is a space, line break, or tab character. If it [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T19:37:01+00:00","article_modified_time":"2024-03-21T16:48:41+00:00","author":"Noah Thompson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Noah Thompson","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/"},"author":{"name":"Noah Thompson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a"},"headline":"How to use recursion in C language to count the number of words","datePublished":"2024-03-15T19:37:01+00:00","dateModified":"2024-03-21T16:48:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/"},"wordCount":306,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/","name":"How to use recursion in C language to count the number of words - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T19:37:01+00:00","dateModified":"2024-03-21T16:48:41+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-recursion-in-c-language-to-count-the-number-of-words\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use recursion in C language to count the number of words"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a","name":"Noah Thompson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","caption":"Noah Thompson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/19903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=19903"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/19903\/revisions"}],"predecessor-version":[{"id":53670,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/19903\/revisions\/53670"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=19903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=19903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=19903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}