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

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: school is boring with schooling and School substring: school output: 1
(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...
Question 1 A substring of String s is a sequence of k >= 0 characters in...
Question 1 A substring of String s is a sequence of k >= 0 characters in s, in the order in which they occur in s. The letters in a substring may be either contiguous (next to each other) or non-contiguous in the original String s. For instance, these are the substrings of String s="abc". String s All substrings of s ======== ================================== "abc" "" "a" "b" "ab" "c" "ac" "bc" "abc" Write a function allSubstrings that returns an ArrayList...
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,...
Implement a substring search function which searches a string for a given keyword. The function should...
Implement a substring search function which searches a string for a given keyword. The function should return a pointer to the address of the first character of the substring within the original string, if it exists. If the substring is not found within the string, return NULL. If the input is found, print "Key found." otherwise print "Key not found." Your program’s input and output should match what is seen in the example run. • You may only use stdio.h....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT