Question

In: Computer Science

Java Write a method that counts how many times one string appears in another string. The...

Java

Write a method that counts how many times one string appears in another string. The method takes three input parameters: two Strings and one Boolean. The Boolean value determines whether the words are allowed to overlap each other. For example:

When the method is called with input parameters (“balloon”, “oo”, false), it returns 1.

When the method is called with input parameters (“hh”, “hhhhhh”, true) returns 5.

When the method is called with input parameters (“cc”, “abcdefg”, true), returns 0.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================


public static int overlaps(String s, String check, boolean overlappingAllowed) {

    int count = 0;
    if (s.length() < check.length()) {
        String temp = s;
        s = check;
        check = temp;
    }
    if (overlappingAllowed) {

        for (int i = 0; i <= s.length() - check.length(); i++) {
            if (s.substring(i, i + check.length()).equals(check)) {
                count += 1;
            }
        }

    } else {

        for (int i = 0; i <= s.length() - check.length(); i++) {
            if (s.substring(i, i + check.length()).equals(check)) {
                count += 1;
                i = i + check.length() - 1;
            }
        }
    }
    return count;
}

===================================================================

// PROGRAM TO TEST WITH THE GIVEN EXAMPELS

public class Overlap {

    public static int overlaps(String s, String check, boolean overlappingAllowed) {

        int count = 0;
        if (s.length() < check.length()) {
            String temp = s;
            s = check;
            check = temp;
        }
        if (overlappingAllowed) {

            for (int i = 0; i <= s.length() - check.length(); i++) {
                if (s.substring(i, i + check.length()).equals(check)) {
                    count += 1;
                }
            }

        } else {

            for (int i = 0; i <= s.length() - check.length(); i++) {
                if (s.substring(i, i + check.length()).equals(check)) {
                    count += 1;
                    i = i + check.length() - 1;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {


        System.out.println(overlaps("balloon","oo",false));
        System.out.println(overlaps("hh","hhhhhh",true));
        System.out.println(overlaps("cc","abcdefg",true));
    }

}


Related Solutions

Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
Write a method in java (with out using startwith,boolean true false) count that counts how many...
Write a method in java (with out using startwith,boolean true false) count that counts how many times a substring occurs in a string:For example: count("is", "Mississippi") will return 2. also add comment besides code.
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT