// 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 ...
-
40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...
-
React Interview Questions React is an efficient, declarative, and flexible open-source JavaScript library for building simple, fast and sc...
No comments:
Post a Comment