• 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 32 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
1 vote
1 answer 80 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
0 votes
1 answer 10 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 5.6k points
0 votes
2 answers 116 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 18 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, 2020 Gavin 15.3k points
0 votes
1 answer 11 views
Problem: Given a string as an input. We need to write a program that will print all non-empty substrings of that given string. Examples : Input : abcd how We can run three nested loops, the outermost loop picks starting character, mid loop considers all characters on right of the picked character as ending character of substring.
asked Nov 5, 2020 Mashhoodch 5.6k points
2 votes
1 answer 20 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, 2020 Gavin 15.3k points
0 votes
1 answer 18 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 9 views
Problem: Hello Kodlogs! I am a beginner and started learning programming through an online course. In course, I am given an assignment in which I have to write a program that will count the occurrence of each character in a string and then print the number of ... with detailed comments so that I can understand the logic and program? The given string is: &ldquo;This is an assignment&rdquo;. Thanks!
asked Jan 6 Code Learner 5.1k points
1 vote
1 answer 15 views
Problem: Hello, good people of Kodlogs, I am a new student of Python programming language. The online course I am taking they are teaching me well. Every weekend they provide me with some problems to solve. This week, I have got the problem find all substrings of a string python. I don&rsquo;t have any idea how to do this! I would appreciate your effort if you pull me up. Many thanks
asked Apr 5, 2020 Gavin 15.3k points