• Register
2 votes
27 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 platform to ask a question. If anybody could reply to my thread and give me a possible solution (with code snippet) would be great for me, and I must recognize your help.
Thanks in advance, mate.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

Hello Gavin,

You’ve asked an interesting question. This is a string data type question regarding java. In the program, our quest is to find out substrings in a string. Let’s try to solve the problem through a simple code below:

public class SubStringCount {
  public static int countTarget(String st, String subst) {
      if (isEmpty(st) || isEmpty(subst)) {
          return 0;
      }
      int counter = 0;
      int index = 0;
      while ((index = st.indexOf(subst, index)) != -1) {
          counter++;
          index += subst.length();
      }
      return counter;
  }

  public static boolean isEmpty(String st) {
      return st == null || st.length() == 0;
  }
}

If you’ve got any question please reply to this thread. Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 62 views
62 views
I am newbee and I was trying some codes in python, I wrote a code in python to count how many times a word appears in a string : string = "Not Your buisness surely not your" CountString = string.count("buisness" 0, 6) print("CountString") It shows an error ... ^ SyntaxError: invalid syntax why am I getting this type of error message and how can I check how many times a word appears in a string?
asked Mar 18, 2020 LizzyM 6.1k points
0 votes
1 answer 4 views
4 views
Problem: I'm having trouble processing this error: It is required that the operation be exported or include an intent-filter “Count total number of times each alphabet appears in the string java program code with example”?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 3 views
3 views
Problem Need help for counting number of times substring appears in string python.
asked Apr 7 salim1 39k points
1 vote
1 answer 2 views
2 views
Problem: Please help me. How can I solve it? python count how many times a word appears in a string
asked Mar 30 MUHAMMAD MUNEEB 83k points
2 votes
1 answer 36 views
36 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. ... problem I’d be grateful to him/her. So, my problem is: find all occurrences of a substring in a string java Thanks, mate.
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
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 48.9k points
1 vote
1 answer 2 views
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 10 views
10 views
Problem: How to find a substring in a string java. Is there any idea?
asked Mar 21 charles mathews 5.5k points
0 votes
1 answer 3 views
3 views
Problem: I can't seem to find a solution; could you please help me “Find if a string is a substring of another java”?
asked Mar 29 tuhin1 48.9k points
1 vote
1 answer 3 views
3 views
Problem: Any ideas on what I need to do to fix this? python count how many times a word appears in a list
asked Mar 30 MUHAMMAD MUNEEB 83k points