• Register
2 votes
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 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 43 views
43 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
2 votes
1 answer 23 views
23 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
2 answers 668 views
668 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
0 votes
1 answer 9 views
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: “This is an assignment”. Thanks!
asked Jan 6 Code Learner 5.4k points
0 votes
1 answer 13 views
13 views
Problem: Hello! I am a beginner, and my teacher gave me an assignment in which I have to write a program that takes a string as input from the user. If the string contains any consecutive duplicate characters then my program should remove all the consecutive occurrences of that ... . I am weak in programming and have almost no idea how I can do that. Can anybody here help me to solve this problem?
asked Jan 2 Code Learner 5.4k points
0 votes
2 answers 116 views
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 37 views
37 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, 2020 LizzyM 6.1k points
0 votes
1 answer 2 views
2 views
Problem
asked 5 days ago alexh 2.2k points
0 votes
1 answer 18 views
18 views
I need to know how to extract numbers from alphanumeric string in Java.
asked Sep 26, 2020 Daniel Anderson 4k points
1 vote
1 answer 18 views
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