| 1. |
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 |
|
Answer» 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); } } |
|