// Java program to count the number // of occurrence of a word in // the given string given string import java.io.*; class GFG { static int countOccurences(String str, String word) { // split the string by spaces in a String a[] = str.split( " " ); // search for pattern in a int count = 0 ; for ( int i = 0 ; i < a.length; i++) { // if match found increase count if (word.equals(a[i])) count++; } return count; } // Driver code public static void main(String args[]) { String str = "GeeksforGeeks A computer science portal for geeks " ; String word = "portal" ; System.out.println(countOccurences(str, word)); } } |
Subscribe to:
Post Comments (Atom)
40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot
40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...
-
Angular 2, 4,5 and 6 interview questions — Set 3 Update: Answers added Angular template can be defined inline as well as ...
-
HLD -- High Level Design (HLD) is the overall system design - covering the system architecture and database design. It describes the relat...
-
Spring Boot Microservices + ELK ( Elasticsearch, Logstash, and Kibana) Stack Hello World Example In this tutorial we will be using ELK st...
No comments:
Post a Comment