What We Are Learn On This Post
Find Matching Substring
The Below Solution prints the total number of occurrences of the substring throughout the String also includes the cases where overlapping matches do exist Without using any predefined methods.
Solution 1:
package com.softwaretestingblog.InterviewPgms; import java.util.Scanner; public class StringIndexof { public static void main(String[] args) { String str="abababababaabb"; String sub = "bab"; int n = str.length(); int m = sub.length(); // index=-1 in case of no match, otherwise >=0(first match position) int index=str.indexOf(sub), i=index+1, count=(index>=0)?1:0; //System.out.println(i+" "+index+" "+count); // i will traverse up to only (m-n) position while(index!=-1 && i<=(n-m)) { index=str.substring(i, n).indexOf(sub); count=(index>=0)?count+1:count; i=i+index+1; //System.out.println(i+" "+index); } System.out.println("Total Number Of Substring Appears: "+count); } }
Find Matching Substring Output:
Total Number Of Substring Appears: 4
Solution 2:
Here is the advanced version for counting how many times the token occurred in a user entered the string.
package com.softwaretestingblog.InterviewPgms; import java.util.Scanner; public class StringIndexof { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a sentence please:"); String string = scanner.nextLine(); int atIndex = 0; int count = 0; while (atIndex != -1) { atIndex = string.indexOf("hello", atIndex); if(atIndex != -1) { count++; atIndex += 5; } } System.out.println("Total Number Of Hello Substring Appears: "+count); } }
Find Matching Substring Output:
Enter a sentence please: helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello Total Number Of Hello Substring Appears: 7
Solution 3:
package com.softwaretestingblog.InterviewPgms; public class StringIndexof { public static void main(String[] args) { String str = "helloslkhellodjladfjhello"; String findStr = "hello"; int count = 0, lastIndex = 0; while((lastIndex = str.indexOf(findStr, lastIndex)) != -1) { lastIndex += findStr.length() - 1; count++; } System.out.println("Total Number Of Hello Substring Appears: "+count); } }
Find Matching Substring Output:
Total Number Of Hello Substring Appears: 3
Solution 4:
Find The Number of Times Substring Appear In String By RegularExpression
- Patterns of arbitrary length
- Overlapping matches (such as counting “232” in “23232” or “aa” in “aaa”)
- Regular expression meta-characters
package com.softwaretestingblog.InterviewPgms; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringIndexof { static int countMatches(Pattern pattern, String string) { Matcher matcher = pattern.matcher(string); int count = 0; int pos = 0; while (matcher.find(pos)) { count++; pos = matcher.start() + 1; } return count; } public static void main(String[] args) { Pattern pattern = Pattern.compile("232"); int count = countMatches(pattern, "23232"); System.out.println("No Of Times Sub String Appears:-"+count); } }
Find Matching Substring Output:
No Of Times Sub String Appears:-2
Solution 5:
package com.softwaretestingblog.InterviewPgms; public class StringIndexof { public static void main(String[] args) { String str = "helloslkhellodjladfjhello"; String findStr = "hello"; int lastIndex = 0; int count = 0; while((lastIndex = str.indexOf(findStr, lastIndex)) != -1) { count++; lastIndex += findStr.length() - 1; } System.out.println("Total Number Of Substring :-"+count); } }
Output:
Total Number Of Substring :-3
Solution 6:
package com.softwaretestingblog.InterviewPgms; public class StringIndexof { public static int getCountSubString(String str , String sub){ int n = 0, m = 0, counter = 0, counterSub = 0; while(n < str.length()){ counter = 0; m = 0; while(m < sub.length() && str.charAt(n) == sub.charAt(m)){ counter++; m++; n++; } if (counter == sub.length()){ counterSub++; continue; } else if(counter > 0){ continue; } n++; } return counterSub; } public static void main(String[] args) { int count; String str = "helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello"; String findStr = "hello"; StringIndexof obj=new StringIndexof(); count=obj.getCountSubString(str, findStr); System.out.println("Total Number Of Substring :-"+count); } }
Output:
Total Number Of Substring :-7
Solution 7:
here is the other solution without using regexp/patterns/matches or even not using StringUtils.
package com.softwaretestingblog.InterviewPgms; public class StringIndexof { public static void main(String[] args) { String str = "helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello"; String findStr = "hello"; int count =0; int findStrLength = findStr.length(); for(int i=0;i<str.length();i++){ if(findStr.startsWith(Character.toString(str.charAt(i)))){ if(str.substring(i).length() >= findStrLength){ if(str.substring(i, i+findStrLength).equals(findStr)){ count++; } } } } System.out.println("Total Number Of Substring :-"+count); } }
Output:
Total Number Of Substring :-7
Go through them and try to learn the best ways to write code and impress the interviewer. Still, if you have any other way of solving the above problem, post in the comment or you can Email Us
Leave a Reply