// 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...
-
1. Introduction In Spring MVC, the DispatcherServlet acts as front controller – receiving all incoming HTTP requests and processing ...
-
HLD -- High Level Design (HLD) is the overall system design - covering the system architecture and database design. It describes the relat...
-
1.A)How HasMap works? HashMap in Java works on hashing principle. It is a data structure which allows us to store obj...
No comments:
Post a Comment