Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to find how many times given string appears in given text as substring.
- * The text is given at the first input line.
- * The search string is given at the second input line.
- * The output is an integer number.
- * Please ignore the character casing. */
- import java.util.Scanner;
- public class _07_CountSubstringOccurrences {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter your target Text on single line:");
- String inputText = scan.nextLine();
- System.out.print("Enter your Word to search: ");
- String searchStr = scan.next();
- String[] text = inputText.toLowerCase().split("[\\W]+");
- searchStr = searchStr.toLowerCase();
- int counter = 0;
- for (String word : text) {
- int indexSearch = word.indexOf(searchStr);
- while (indexSearch >= 0) {
- counter++;
- indexSearch = word.indexOf(searchStr, indexSearch + 1);
- }
- }
- System.out.println("Given String appears in Given Text as Substring "
- + counter + " many times!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement