{"id":4245,"date":"2017-10-27T00:46:45","date_gmt":"2017-10-26T19:16:45","guid":{"rendered":"https:\/\/www.java2blog.com\/?p=4245"},"modified":"2023-02-07T12:40:58","modified_gmt":"2023-02-07T07:10:58","slug":"java-program-count-number-words-string","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-program-count-number-words-string\/","title":{"rendered":"Count number of words in a string"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Problem\">Problem<\/a><\/li><li><a href=\"#Algorithm\">Algorithm<\/a><\/li><li><a href=\"#Program\">Program<\/a><\/li><\/ul><\/div>\n<p>In this post, we will see how to find number of words in a String.<\/p>\n<h2><span id=\"Problem\"><span style=\"color: #f89820;\">Problem<\/span><\/span><\/h2>\n<p>Find the number of words in a String.<\/p>\n<p><strong>For example:<\/strong><br \/>\nThere are 6 words in below String<\/p>\n<div class=\"content-box-green\">welcome to java tutorial on Java2blog<\/div>\n<h2><span id=\"Algorithm\"><span style=\"color: #f89820;\">Algorithm<\/span><\/span><\/h2>\n<p>The algorithm will be very simple.<\/p>\n<ul>\n<li>Initialize count with 1 as if there are no spaces in the string, then there will be one word in the String.<\/li>\n<li>Check if you encounter any space.<\/li>\n<li>Once you find the space, check it next character. If it is not space then we found a word in the String.Increment the count variable.<\/li>\n<li>Once you reach end of String, count variable will hold number of words in the String.<\/li>\n<\/ul>\n<h2><span id=\"Program\"><span style=\"color: #f89820;\">Program<\/span><\/span><\/h2>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog.java8;\n\npublic class CountNumberOfWordsInStringMain {\n\n    public static void main(String[] args) {\n        String str = \"welcome to java   tutorial on Java2blog\";\n\n        int count = 1;\n\n        for (int i = 0; i &lt; str.length() - 1; i++)\n        {\n            if ((str.charAt(i) == ' ') &amp;&amp; (str.charAt(i + 1) != ' '))\n            {\n                count++;\n            }\n        }\n        System.out.println(\"Number of words in a string : \" + count);\n    }\n}\n<\/pre>\n<p>When you run above program, you will get below output:<\/p>\n<div class=\"content-box-green\">Number of words in a string : 6<\/div>\n<p>that&#8217;s all about how to count number of words in a string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsProblemAlgorithmProgram In this post, we will see how to find number of words in a String. Problem Find the number of words in a String. For example: There are 6 words in below String welcome to java tutorial on Java2blog Algorithm The algorithm will be very simple. Initialize count with 1 as if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[151,144,27],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4245"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=4245"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4245\/revisions"}],"predecessor-version":[{"id":22804,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4245\/revisions\/22804"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=4245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=4245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=4245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}