Question

In: Computer Science

use java recursion find # of times a substring is in a string but it has...

use java recursion

find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters.

input is 2 strings, output is an int

input: ("Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "hello")

should output: 3

input: (" Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "Hello")

should output: 1

input: (" Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello Hello ", "Hello")

should output: 2

input: (" HELLO it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello Hello", "Hello")

should output: 1

input: (" HELLO it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello Hello", "")

should output: 0

Solutions

Expert Solution

public class SubstringRecursion {

        public static void main(String[] args) {
                
                int times;
                times = substring("Hello World Welcome to new World","",0);
                System.out.println(times);
                times = substring("king of Kings","king",0);
                System.out.println(times);
                
                String s = "I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy";
                times = substring(s, "happy", 0);
                System.out.println(times);

        }
        
        
        public static int substring(String string, String substring, int index) {
                
                if(substring.length()==0||string.length()==0)
                        return 0;
                // if it has no matching then return 0
                if(string.indexOf(substring)== -1) {
                        return 0;
                }
                
                // if string found we have to start checking from next character
                int i = string.indexOf(substring)+1;
                
                // adding 1 to new substring check
                return 1 + substring(string.substring(i), substring, index + 1);
        }
        

}

Code

Output


Related Solutions

(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: Write a driver program that calls this method from the Main program. • • Write a driver program that calls this method from the Main program. Note: Your program name should match the name of your java /...
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
Please provide a detailed walk through osf this Java application, which uses recursion to find the...
Please provide a detailed walk through osf this Java application, which uses recursion to find the maximal (largest) contiguous sum in a list of integers. Base code on the algorithm below. Input Read from a text file (List.dat) lines of data where each line represents a list in this format: list-size   numbers in the list separated by blanks 4          100 -50 5 8 For example, List.dat might contain: 7    -2   -4   30    15    -7    -5    1000 2  -50  100 6  1000  -2000   900   2800   -2900    2801 0   4  100   -10   5   8 4  100   -50   5   8 Note: the list-size must be greater than 0. Bypass...
Java:    Find a pattern that will match any string that is --  at least 6 characters...
Java:    Find a pattern that will match any string that is --  at least 6 characters long, -- and begins with a letter or number (\w) -- and contains at least one non-letter and non-number (\W).
I'm working on python code which is substring matching. For example, the given string is input...
I'm working on python code which is substring matching. For example, the given string is input "CTTGTGATCTCGTGTCGTGGGTAG", and a substring we want to find in the main one is "GTGG". So the code will print out the position and the substrings in the main which has exactly one different position. For example, start at position 4 the substring is GTGA since there is only one letter different, and so on. Even though we know that string index starts at 0,...
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT