Question

1.This is a problem from our midterm. HOWEVER, you must use functions to solve the problem...

1.This is a problem from our midterm. HOWEVER, you must use functions to solve the problem this time.(using Java please!)

Write a program that prompts the user to enter two strings s1 and s2 and reports whether s2 is a substring of s1. The matching should be case-sensitive. The ONLY String class methods you are allowed to use is charAt(index) and length().

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE in Java:

import java.util.Scanner; // For input from user.

public class CheckSubString{

// Method for checking if s2 is a substring of s1 or not.
// It returns true if s2 is a substring of s1 otherwise false.
public static boolean checkSubString(String s1, String s2)
{
// for keeping track of s2 if it is matching completely or not
// character by character.
boolean checkSS = false;
  
// loop from starting character of s1 to the character where length
// of s2 can be fitted. Like if
// s1 is "checkcheck" and s2 is "abcde"
// As length of s2 is 5 so we will check in s1 till length where 5
// character can be adjusted so we will check till 10 - 5 + 1 = 6 so
// index will be 1 less than 6 which will be 5. So, we will check till C
// which has index 5.
for(int i=0; i<s1.length()-s2.length()+1; i++)
{
checkSS = false;
  
if(s1.charAt(i) == s2.charAt(0))
{
for(int j=0; j<s2.length(); j++)
{
if(s1.charAt(i+j) == s2.charAt(j))
checkSS = true; // keep assigning it true if characters are matching.
else
{
// if one of the characters doesn't match then change checkSS to false
// and break the loop as there is no need to compare it further.
checkSS = false;
break;
}
}
}
  
// if checkSS becomes true means we already find that s2 is substring of s1
// so just break the outer loop. No need to traverse the whole string.
if(checkSS)
break;
}
  
// Just return the checkSS value.
return checkSS;
}
  
public static void main(String []args)
{
String s1, s2; // Define the strings.
  
Scanner sc = new Scanner(System.in); // For input.
  
// For inputting String s1.
System.out.println("Enter String s1: ");
s1 = sc.nextLine();
  
// For inputting String s2.
System.out.println("Enter String s2: ");
s2 = sc.nextLine();
  
// if method returns true then print it is a substring otherwise not.
if(checkSubString(s1,s2))
System.out.println("s2 is a substring of s1.");
else
System.out.println("s2 is not a substring of s1.");
  
}
}

CODE Screenshots:

OUTPUT Screenshots:

(Note: On left are the s1 and s2 and right side contains the output.)

Add a comment
Know the answer?
Add Answer to:
1.This is a problem from our midterm. HOWEVER, you must use functions to solve the problem...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need answer in Python 3.8, with indentation and comments shown, please. 16.4* (Pattern matching) Write a...

    Need answer in Python 3.8, with indentation and comments shown, please. 16.4* (Pattern matching) Write a program that prompts the user to enter two strings and tests whether the second string is a substring in the first string. (Don't use the find method in the str class.) Analyze the time complexity of your algorithm. Here is a sample run of the program <output> Enter a string s1: Mississippi Enter a string s2: sip matched at index 6 <End Output>

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • Write Java Programs to perform the following: 1. To create a new String object 2. To...

    Write Java Programs to perform the following: 1. To create a new String object 2. To find the length of the string 3. Concatenation operator 4. To display the content in String array 5. A complete Java Program that incorporates the following String methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length() e. compareTo(s1)) methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length()) e. compareTo(s1)) f. 51.substring(4)) g. s2.substring(2,10)) h. s1.indexOf('g')) i....

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • JAVA Programming Produce a method that reads in a set of values (double) from the user...

    JAVA Programming Produce a method that reads in a set of values (double) from the user and returns them. Use this header: public static double[] getNumsFromUser(String msg1, String msg2) The implementation of the method should start with a message to input the total number of array elements, followed by a message to enter all the array elements. The method returns the array of elements. The two strings msg1 and msg2 that are passed to the method as parameters will be...

  • use C language please Problem 5. (5 points] Use string functions. Write a program that: 1)...

    use C language please Problem 5. (5 points] Use string functions. Write a program that: 1) Ask the user to write two strings. 2) If both strings are equal, print an appropriate message then print out both strings with their length. 3) If the strings are not equal, print an appropriate message then concatenate both strings and print out the length of the concatenating strings. 4) In the main function prints both arrays.

  • JAVA Problem Description: For this assignment, you will be writing your own exception class and you...

    JAVA Problem Description: For this assignment, you will be writing your own exception class and you will handle run-time exceptions resulting from one particular input situation. Specifics: 1) Design and implement a program that has an exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. 2) In the main driver of the program (call this class MyExceptionTest), read strings from the user until the user enters “DONE”. If a...

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT