Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview In this assignment you will write a program that will accept a string a

ID: 3748625 • Letter: O

Question

Overview In this assignment you will write a program that will accept a string and a search term from the user. Your program will then ask the user if they want to consider spaces and if upper and lower case should be considered as different. All of these will be accepted as strings and int values will be parsed as needed. It then tells the user if the search term is in the string or not, ignoring spaces and case as asked, and where the term was in the string Requirements Your program must do the following in order to receive full credit on this assignment 1. Accept two string values from the user. a. The first is the original string to search through. b. The second is the substring we will be searching the first string for. 2. Ask the user if they want to consider spaces in the two strings. Ask the user to type a number for Yes (like 1) and a second number for No. b. a. This must be pulled from the Scanner as a String! 3. Ask the user if they want to consider differences between upper and lower case in the two strngs Again, ask for a number. Again, this must be taken in as a String a. b. 4. Convert the two number inputs into their int values and store them in new int variables. You do not need to do any checking if they actually typed in an int or not for this assignment. You can also assume that they typed in one of the numbers that you asked for. a. b. If the user asked you to not consider spaces, remove them from both the original string and the search term 5. a. Hint: Remember that replace allows you to replace with a blank string. 6. If the user asked you to not consider case, convert both the original string and search term strings to the same case You might want to just use equalslgnoreCase, but some of the comparisons we will be making cannot ignore the case, so we must set it all to the same case in order to avoid that problem a.

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


import java.util.Scanner;

public class SearchString {

public static void main(String[] args) {
String first, second;
Scanner keyboard = new Scanner(System.in);
int spaces, caseSensitive;
String input;


System.out.println("Please input a word or phrase");
first = keyboard.nextLine();

System.out.println("Please input the string you would like to search for in your word or phrase");
second = keyboard.nextLine();

System.out.println("Do you want to consider spaces? Type 1 for Yes, or 0 for No.");
input = keyboard.next();
spaces = Integer.parseInt(input);


System.out.println("Do you want to consider upppercase and lowercase as different? Type 1 for Yes, or 0 for No.");
input = keyboard.next();
caseSensitive = Integer.parseInt(input);


if(spaces == 0){
first = first.replaceAll(" ", "");
second = second.replaceAll(" ", "");
}

if(caseSensitive == 0){
first = first.toLowerCase();
second = second.toLowerCase();
}

if(first.equals(second))
System.out.println("Your search is equal to entire input.");
else if(first.startsWith(second))
System.out.println("Your search appears in the beginning of input");
else if(first.endsWith(second))
System.out.println("Your search appears in the end of the input");
else{
int index = first.indexOf(second);
if(index == -1)
System.out.println("Your search does not appear in the input");
else
System.out.println("Your search appears at index " + index + " of the input");
}
}

}


output
------
Please input a word or phrase
cheese is good
Please input the string you would like to search for in your word or phrase
cheese
Do you want to consider spaces? Type 1 for Yes, or 0 for No.
1
Do you want to consider upppercase and lowercase as different? Type 1 for Yes, or 0 for No.
1
Your search appears in the beginning of input