Write a program that reads a line and a substring. It should then display the number of occurrence of the given substring in the line
Answers
Answered by
0
Program :
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
String sentence , word;
System.out.print("Enter a sentence : ");
sentence = Sc.nextLine();
System.out.print("Enter a word : ");
word = Sc.next();
String a[] = sentence.split(" ");
int count = 0;
for(int i = 0 ; i < a.length ; i++)
{
System.out.println(a[i]);
if(word.equalsIgnoreCase(a[i]))
{
count++;
}
}
System.out.print("Word count = " + count);
}
}
Similar questions