How to find all occurrences of a substring in a string java

35 views

Problem:

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.

1 vote

Solution:

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)) {
break;
}
}
if (n == i) {
res++;
n = 0;
}
}
return res;
}

static public void main(String[] args) {
String txt = "naymulhasan";
String pat = "san";
System.out.println(countFreq(pat, txt));
}
} ``````

Related questions

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
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?
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;?
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.
1 vote
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?
1 vote
2 views
Problem; Need help with the count of substring in Java. Count the number of occurrences of substring in a string in java.
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
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!
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!