Advertisement
DNNdrago

7. Count Substring Occurrences

May 23rd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.49 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class _07_CountSubstringOccurrences {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner (System.in);
  8.         String text = sc.nextLine().toLowerCase();
  9.         String target = sc.nextLine().toLowerCase();
  10.        
  11.         int countOfRepeats = 0;
  12.        
  13.         for (int i = 0; i <= text.length() - target.length(); i++) {
  14.             if (text.substring(i, i + target.length()).equals(target))
  15.                 countOfRepeats++;
  16.         }
  17.        
  18.         System.out.println(countOfRepeats);
  19.  
  20.     }
  21.  
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement