Advertisement
a1m

Count Substring Occurrences

a1m
Sep 10th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Problem_07_CountSubstringOccurrences {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String text = scanner.nextLine();
  7.         String searchedString  = scanner.nextLine();
  8.         int stringCounter = 0;
  9.         int stringIndex = (text.toLowerCase().indexOf(searchedString.toLowerCase()));
  10.         while (stringIndex != -1){
  11.             stringCounter++;
  12.             stringIndex = (text.toLowerCase().indexOf(searchedString.toLowerCase(),stringIndex+1));
  13.         }
  14.         System.out.println(stringCounter);
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement