Tuesday 22 January 2019

ITV-programe-1-No of occurrence in a string.


// 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));
}
}

No comments:

Post a Comment

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...