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: school is boring with schooling and School

substring: school

output: 1

Solutions

Expert Solution

import java.util.*;
class Main 
{ 
    //recursive function to count occurance of substrig
    static int countSubstr(String str1, String str2) 
    { 
        //length of input string
        int n1 = str1.length(); 
        
        //length of substring
        int n2 = str2.length(); 
    
        //base case 
        if (n1 == 0 || n1 < n2) 
                return 0; 
    
        //recursive case 
        //checkes if the first substring match or not
        if (str1.substring(0, n2).equals(str2)) 
                return countSubstr(str1.substring(n2 - 1), str2) + 1; 
    
        //otherwise, return the count from the remaining index 
        return countSubstr(str1.substring(n2 - 1), str2); 
    } 

    //main function
    public static void main(String args[]) 
    { 
        //to store input string and substring
        String input, substring;
        Scanner sc = new Scanner(System.in);
        
        //input String
        System.out.print("Input: ");
        input = sc.nextLine();
        
        
        //substring
        System.out.print("substring: ");
        substring = sc.nextLine();
        
        int count = countSubstr(input, substring);
        //function call and prints counts
        System.out.println("output: " + count);
    } 
} 

Please refer below screenshot of code for better understanding of indentation.

Sample 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 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...
(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 /...
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...
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...
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).
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT