• Register
2 votes


Hello guys,

I hope you all are doing well. I am learning java programming in my school. My teacher taught us many things about the string in the last class and gave a problem to solve. I tried to solve the problem but couldn’t make it happen so far. I found this website very helpful and friendly. If anybody could help me to solve this problem I’d be grateful to him/her.

So, my problem is: find all occurrences of a substring in a string java

Thanks, mate.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


Finding the substring from a string is an interesting thing to do. There are two ways to do it, one is Naive pattern searching another one is KMP algorithm

I think the first one is pretty easier for the beginners. So, let’s practice with the Naive pattern searching: 

public class SubString { 
    static int countFreq(String pat, String txt) {         
        int i = pat.length();         
        int j = txt.length();         
        int res = 0; 
        for (int m = 0; m <= i - j; m++) { 
            int n;             
            for (n = 0; n < i; n++) { 
                if (txt.charAt(m + n) != pat.charAt(n)) { 
            if (n == i) {                 
                n = 0;                 
        return res;         
    static public void main(String[] args) { 
        String txt = "naymulhasan";         
        String pat = "san";         
        System.out.println(countFreq(pat, txt));         

To know more about string algorithms click here

13 9 6
94,240 points

Related questions

0 votes
1 answer 40 views
Problem: I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is: def DeletChar(e, b) : counts = e.count(b) e = list(e) while counts : e.remove(b) counts -= 1 print(e) if __name__ == '__main__' : e = "geeksforgeeks" DeletChar(e,'g') The output ... e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's'] how to solve this problem I am a newbee please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 3 views
Problem: As I mentioned in my previous query, I'm having difficulties with my learning; I've tried searching the web but haven't been able to find adequate information; could you assist me in continuing my project?
asked Mar 29 tuhin1 47.2k points
0 votes
1 answer 2 views
Problem: Do you have any suggestions about how I can resolve this &ldquo;Find all occurrences of a substring in a string&rdquo;?
asked Mar 29 tuhin1 47.2k points
0 votes
1 answer 16 views
Problem: The question is to traverse given string, as soon as we find an occurrence of we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
asked Nov 2, 2020 Mashhoodch 13k points
1 vote
1 answer 114 views
Problem: I wrote this code to find all occurrences of a substring in a string python: import re test_str = "all in all is not the all of all " test_sub = "all" print("The main string is : " + test_str) print("The substring to find : " + test_sub) print(" ... is not defined The first two ouput of this code is right but it throws error message after that. What should I do to run this code properly?
asked Apr 22, 2020 samhaz 5k points
1 vote
1 answer 2 views
Problem; Need help with the count of substring in Java. Count the number of occurrences of substring in a string in java.
asked Mar 29 PkGuy 13.1k points
0 votes
1 answer 5 views
Problem: Hello ... Can anyone tell me that why I got this error? program to count number of characters in a string in c
asked Mar 25 Ifra 24.4k points
0 votes
1 answer 8 views
Problem: how to I Solve this: error: count number of characters in string python HELP! I need help understanding this: count number of characters in string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 13 views
Problem: How to write a program that finds the duplicate words and their number of occurrences in a string? I am a beginner with no prior experience in programming so facing some difficulty in developing logic. Please help. Thanks!
asked Feb 28 Code Learner 9.2k points
0 votes
1 answer 2 views
Problem: Linux count occurrences of string in file.
asked 2 days ago charles mathews 5.5k points