<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":649,"date":"2021-07-13T23:34:18","date_gmt":"2021-07-13T23:34:18","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=649"},"modified":"2021-09-11T11:14:46","modified_gmt":"2021-09-11T11:14:46","slug":"how-to-count-occurrence-of-a-given-character-in-a-string-in-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/","title":{"rendered":"How to Count Occurrence of a given Character in a String in Java ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-count-occurrence-of-a-given-character-in-a-string-in-java\" class=\"color-pink\" style=\"text-align: justify;\">How to Count Occurrence of a given Character in a String in Java ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly. The following steps are given below,\n<ul>\n<li>First, we split the strings by spaces.<\/li>\n<li>Take a variable count = 0 and in every true condition we increment the count by 1.<\/li>\n<li>Now run a loop at 0 to\u00a0length of string.<\/li>\n<li>And then check if our string is equal to the word.<\/li>\n<li>If condition true then we increment the value of count by 1.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\"><\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"sample-code-in-java\" class=\"color-blue\" style=\"text-align: justify;\">Sample Code in Java<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">class wiki{<br\/>\tpublic static void main (String[] args) {<br\/>\tString str = &quot;Welcome to wikitechy website. Wikitechy is the best website to learn technology.&quot;<br\/>String findStr = &quot;wikitechy&quot;;<br\/>int lastIndex = 0;<br\/>int count = 0;<br\/><br\/>while(lastIndex != -1){<br\/><br\/>    lastIndex = str.indexOf(findStr,lastIndex);<br\/><br\/>    if(lastIndex != -1){<br\/>        count ++;<br\/>        lastIndex += findStr.length();<br\/>    }<br\/>}<br\/>System.out.println(count);<br\/>\t}<br\/>}<\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"CodeContent\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code id=\"code1\" class=\"hljs\" data-lang=\"\"><span class=\"nt\">2<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : The number of occurrences of each character to be count&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[4004],"tags":[195,491,360,1858,4220,4221,203,199,214,198,363,209,4216,4215,4222,4224,205,2936,222,4219,4218,196,212,213,15932,4217,286,207,366,204,217,282,4023,483,206,200,197,280,364,968,4223],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-java","tag-accenture-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-c-program-to-count-number-of-characters-in-a-string","tag-c-program-to-find-frequency-of-characters-in-a-string","tag-c-program-to-count-the-number-of-occurrences-of-a-character-in-a-string","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-count-occurrences-of-each-character-in-string-java","tag-count-repeated-characters-in-string-using-java","tag-count-the-number-of-repeated-characters-in-a-given-string-in-c","tag-count-total-number-of-times-each-alphabet-appears-in-the-string-java-program-code-with-example","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-fis-global-business-solutions-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-how-to-count-occurrence-of-a-given-character-in-a-string-in-java","tag-how-to-count-the-number-of-occurrences-of-each-character-in-a-string-in-java","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-infrrd-interview-questions-and-answers","tag-java-program-to-count-number-of-alphabets-in-a-string","tag-lt-infotech-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-rbs-india-de-interview-questions-and-answers","tag-reliance-industries-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-wipro-infotech-interview-questions-and-answers","tag-write-a-java-program-to-count-number-of-characters-in-given-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Count Occurrence of a given Character in a String in Java ?<\/title>\n<meta name=\"description\" content=\"How to Count Occurrence of a given Character in a String in Java ? - The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly.\" \/>\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.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Count Occurrence of a given Character in a String in Java ?\" \/>\n<meta property=\"og:description\" content=\"How to Count Occurrence of a given Character in a String in Java ? - The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T23:34:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-11T11:14:46+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/\",\"name\":\"How to Count Occurrence of a given Character in a String in Java ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-13T23:34:18+00:00\",\"dateModified\":\"2021-09-11T11:14:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to Count Occurrence of a given Character in a String in Java ? - The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Count Occurrence of a given Character in a String in Java ?","description":"How to Count Occurrence of a given Character in a String in Java ? - The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly.","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.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/","og_locale":"en_US","og_type":"article","og_title":"How to Count Occurrence of a given Character in a String in Java ?","og_description":"How to Count Occurrence of a given Character in a String in Java ? - The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T23:34:18+00:00","article_modified_time":"2021-09-11T11:14:46+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/","name":"How to Count Occurrence of a given Character in a String in Java ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-13T23:34:18+00:00","dateModified":"2021-09-11T11:14:46+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to Count Occurrence of a given Character in a String in Java ? - The number of occurrences of each character to be count and also the occurrences of duplicate characters it should not be display repeatedly.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-count-occurrence-of-a-given-character-in-a-string-in-java\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":4,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":3406,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/649\/revisions\/3406"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}