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
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
Given a string str of even length, print out the first half.   Methods to use: substring(),...
Given a string str of even length, print out the first half.   Methods to use: substring(), length() Testing cases: WooHoo => Woo HelloThere   => Hello abcdef => abc ab   => a 0123456789   => 01234 kitten   => kit (Note that the sample input test cases are shown before the “=>” and the expected output each test case is after the “=>”. Your program should print out the expected output only, not the input.) this is for java language
(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...
Assume s is a string of numbers. Write a program that prints the longest substring of...
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 order...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT