• 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

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 19 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 LizzyM 6.1k points
1 vote
1 answer 47 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 samhaz 5k points
0 votes
2 answers 103 views
Problem : How to count the number of occurrences of a character in a string in java
asked Oct 23, 2019 peterlaw 6.9k points
1 vote
1 answer 10 views
Problem: Hello All, I am a novice in Java programming. I am learning Java by taking an online class out of my own curiosity. I am solving basic level problems from many websites. So far, I was doing good. I get stuck as soon as I get a problem that asked ... characters in a string but how to solve this particular problem? Do you guys have any idea? Please, help me to solve the problem. Thanks.
asked Apr 14 Gavin 15.3k points
2 votes
1 answer 16 views
Problem: I just started learning java and solving problems from various online websites. Stuck on a problem saying, count how many times a substring appears in a string java. This problem is beyond my understanding. I found Kodlogs very helpful and friendly environment ... possible solution (with code snippet) would be great for me, and I must recognize your help. Thanks in advance, mate.
asked Mar 25 Gavin 15.3k points