Assignment 2 Overview In this assignment you will write a program that will acce
ID: 3748529 • Letter: A
Question
Assignment 2 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. a. Ask the user to type a number for Yes (like 1) and a second number for No. b. 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 strings a. Again, ask for a number b. Again, this must be taken in as a String 4. Convert the two number inputs into their int values and store them in new int variables b. You can also assume that they typed in one of the numbers that you asked for. 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. a. You might want to just use equalsIgnoreCase, 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.Explanation / Answer
Please find the solution to your question in Java marked in bold. Let me know if you have any concerns.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//Solution to Question no 1 Starts Here
System.out.println("Enter the string to be searched");
String toBeSearched=br.readLine(); //Accepting string to be searched string from the user
System.out.println("Enter the input string");
String inputString= br.readLine();//Accepting input string from the user
//Solution to Question no 1 ends Here
//Solution to Question no 2,3,5,6,7 starts Here
Scanner sc = new Scanner(System.in);
System.out.println("Do you want to consider spaces in the input String,please enter Y for Yes N for No");
String yesOrNo= sc.next();// Taking the userInput
String inputStringingoreSpaces=null;
StringBuilder inputStringwithoutSpaces = new StringBuilder();
String toBeSeacrcjedingoreSpaces=null;
StringBuilder toBeSeacrcjedwithoutSpaces = new StringBuilder();
String sameCaseInputString=null;
StringBuilder sameCaseInputStringBuilder = new StringBuilder();
String sameCaseSearchedString=null;
StringBuilder sameCaseSearchedStringBuilder = new StringBuilder();
if(yesOrNo.equalsIgnoreCase("Y")) {// If the user asks us to ignore space,split the input String and the string
String[] sip= inputString.trim().split("\s+");// to be searched on the basis of space ,split function is used for the
for(int i=0;i<sip.length;i++) {//purpose
inputStringwithoutSpaces.append(sip[i]);//Append all the splitted parts for input string in the stringBuilder inputStringwithoutSpaces
}
inputStringingoreSpaces=inputStringwithoutSpaces.toString();//get the inputString withoutSpaces named inputStringingoreSpaces
String[] tbs= inputString.trim().split("\s+");
for(int i=0;i<tbs.length;i++) {
toBeSeacrcjedwithoutSpaces.append(tbs[i]);//Append all the splitted parts for string to be searched in in the stringBuilder
}
toBeSeacrcjedingoreSpaces=toBeSeacrcjedwithoutSpaces.toString();//get the inputString withoutSpaces named toBeSeacrcjedingoreSpaces
System.out.println("Do you want to consider cases in the strings,Please enter Y for Yes and N for No.");
String OkorNo=sc.next();// Taking the userinput if the userwants to consider the case of string while searching
if(OkorNo.equalsIgnoreCase("N")||OkorNo.equalsIgnoreCase("Y")) {//either for Y or N
for(int i=0;i<inputStringingoreSpaces.length();i++) {
if( inputStringingoreSpaces.charAt(i)>='a'&& inputStringingoreSpaces.charAt(i)<='z') {//converting the input string
sameCaseInputStringBuilder.append(inputStringingoreSpaces.charAt(i)-'a'+'A');//and string to be seached to same case
}
else
sameCaseInputStringBuilder.append(inputStringingoreSpaces.charAt(i));
}
sameCaseInputString = sameCaseInputStringBuilder.toString();// getting the same case input string in string sameCaseInputString
for(int i=0;i<toBeSeacrcjedingoreSpaces.length();i++)//converting the string to be searched to the same case
{
if( toBeSeacrcjedingoreSpaces.charAt(i)>='a'&& toBeSeacrcjedingoreSpaces.charAt(i)<='z') {
sameCaseSearchedStringBuilder.append(toBeSeacrcjedingoreSpaces.charAt(i)-'a'+'A');
}
else
sameCaseSearchedStringBuilder.append(toBeSeacrcjedingoreSpaces.charAt(i)-'a'+'A');
}
sameCaseSearchedString=sameCaseSearchedStringBuilder.toString();// getting the same case input string in string sameCaseSearchedString
/////Part 7
//if toBeSearched String equals entire InputString
if(sameCaseSearchedString.equals(sameCaseInputString)) {
System.out.println("Input String is same as String to be searched");
}
//If string to be searched is not in input String
else if(!sameCaseInputString.contains(sameCaseSearchedString)) {
System.out.println("Input String is doesnot contain String to be searched");
}
else {
//If the string to be searched is present in input String
int startindex= sameCaseInputString.indexOf(sameCaseSearchedString);
int endindex =startindex+ sameCaseSearchedString.length();
System.out.println("String to be searched starts at index :"+ startindex+" and ends at "+ endindex);
}
}
}
else {
//if the spaces are to be considered
System.out.println("Do you want to consider cases in the strings,Please enter Y for Yes ,N for No");
String OkorNo=sc.next();// Taking the userinput if the user wants to consider the case of string while searching
if(OkorNo.equalsIgnoreCase("N")||OkorNo.equalsIgnoreCase("Y")) {
for(int i=0;i<inputString.length();i++) {//Converting the inputString to same case and storing in string sameCaseInputString
if( inputString.charAt(i)>='a'&& inputString.charAt(i)<='z') {
sameCaseInputStringBuilder.append(inputString.charAt(i)-'a'+'A');
}
else
sameCaseInputStringBuilder.append(inputString.charAt(i));
}
sameCaseInputString = sameCaseInputStringBuilder.toString();
for(int i=0;i<toBeSearched.length();i++)
{
if( toBeSearched.charAt(i)>='a'&& toBeSearched.charAt(i)<='z') {//Converting the inputString to same case and storing in string sameCaseInputString
sameCaseSearchedStringBuilder.append(toBeSearched.charAt(i)-'a'+'A');
}
else
sameCaseSearchedStringBuilder.append(toBeSearched.charAt(i)-'a'+'A');
}
sameCaseSearchedString=sameCaseSearchedStringBuilder.toString();
}
/////Part 7
//if toBeSearched String equals entire InputString
if(sameCaseSearchedString.equals(sameCaseInputString)) {
System.out.println("Input String is same as String to be searched");
}
//If string to be searched is not in input String
else if(!sameCaseInputString.contains(sameCaseSearchedString)) {
System.out.println("Input String is doesnot contain String to be searched");
}
else {
//If the string to be searched is present in input String
int startindex= sameCaseInputString.indexOf(sameCaseSearchedString);
int endindex =startindex+ sameCaseSearchedString.length();
System.out.println("String to be searched starts at index :"+ startindex+" and ends at "+ endindex);
}
}
////Solution to Question no 2,3,5,6,7 ends Here
//Solution to Question Number 4
//Storing the Input string and string to be searched in inputStringInt array and tobeSearchedInt array respectively.
System.out.println("Converting the given two strings to their int variable");
int inputStringInt[] = new int[inputString.length()];
int tobeSearchedInt[] = new int[toBeSearched.length()];
for(int i=0;i<inputString.length();i++)//Converting the inputString to int array inputStringInt
{ if( inputString.charAt(i)>='a'&& inputString.charAt(i)<='z')
inputStringInt[i] = inputString.charAt(i)-'a'+97;
else if( inputString.charAt(i)>='A'&& inputString.charAt(i)<='Z') {
inputStringInt[i] = inputString.charAt(i)-'A'+65;
}
else {
inputStringInt[i]= Integer.parseInt(Character.toString(inputString.charAt(i)));
}
}
for(int i=0;i<toBeSearched.length();i++) //Converting the string to be searched to int array tobeSearchedInt
{
if( toBeSearched.charAt(i)>='a'&& toBeSearched.charAt(i)<='z')
tobeSearchedInt[i] = toBeSearched.charAt(i)-'a'+97;
else if( toBeSearched.charAt(i)>='A'&& toBeSearched.charAt(i)<='Z') {
tobeSearchedInt[i] = inputString.charAt(i)-'A'+65;
}
else {
tobeSearchedInt[i]= Integer.parseInt(Character.toString(toBeSearched.charAt(i)));
}
}
}
}