• Register
2 votes
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 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

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 31 views
31 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 LizzyM 6.1k points
2 votes
1 answer 17 views
17 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 Gavin 15.3k points
0 votes
2 answers 103 views
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
0 votes
2 answers 393 views
393 views
Problem : I am facing following issue need solution return true if the string "cat" and "dog" appear the same number of times in the given string
asked Nov 13, 2019 peterlaw 6.9k points
1 vote
1 answer 31 views
31 views
Problem: I am new in python programming language and I have been doing a online python course. I am doing my coding practice. I tried to check out in a string how many time word does exist or appear. I wrote this code to check this thing: test = "If you do not ... 3 Appearence = test.count("do" 0, 100) ^ SyntaxError: invalid syntax Can anyone help me out to solve this thing. Thank you in advance.
asked Apr 7 LizzyM 6.1k points