Welcome to Westonci.ca, your go-to destination for finding answers to all your questions. Join our expert community today! Explore a wealth of knowledge from professionals across different disciplines on our comprehensive platform. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.

Your goal is to write a JAVA program that will ask the user for a long string and then a substring of that long string. The program then will report information on the long string and the substring. Finally it will prompt the user for a replacement string and show what the long string would be with the substring replaced by the replacement string. Each of the lines below can be produced by using one or more String methods - look to the String method slides on the course website to help you with this project.
A few sample transcripts of your code at work are below - remember that your code should end with a newline as in the textbook examples and should produce these transcripts exactly if given the same input. Portions in bold indicate where the user has input a value.
One run of your program might look like this:
Enter a long string: The quick brown fox jumped over the lazy dog
Enter a substring: jumped
Length of your string: 44
Length of your substring: 6
Starting position of your substring: 20
String before your substring: The quick brown fox
String after your substring: over the lazy dog
Enter a position between 0 and 43: 18
The character at position 18 is x
Enter a replacement string: leaped
Your new string is: The quick brown fox leaped over the lazy dog
Goodbye!
A second run with different user input might look like this:
Enter a long string: Friends, Romans, countrymen, lend me your ears
Enter a substring: try
Length of your string: 46
Length of your substring: 3
Starting position of your substring: 21
String before your substring: Friends, Romans, coun
String after your substring: men, lend me your ears
Enter a position between 0 and 45: 21
The character at position 21 is t
Enter a replacement string: catch
Your new string is: Friends, Romans, councatchmen, lend me your ears
Goodbye!


Sagot :

Answer:

In  Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 String longstring, substring;

 System.out.print("Enter a long string: ");

 longstring = input.nextLine();

 System.out.print("Enter a substring: ");

 substring = input.nextLine();

 

 System.out.println("Length of your string: "+longstring.length());

 System.out.println("Length of your substring: "+substring.length());

 System.out.println("Starting position of your substring: "+longstring.indexOf(substring));

 

 String before = longstring.substring(0, longstring.indexOf(substring));

 System.out.println("String before your substring: "+before);

 

 String after = longstring.substring(longstring.indexOf(substring) + substring.length());

 System.out.println("String after your substring: "+after);

 

 System.out.print("Enter a position between 0 and "+(longstring.length()-1)+": ");

 int pos;

 pos = input.nextInt();

 System.out.println("The character at position "+pos+" is: "+longstring.charAt(pos));

 

 System.out.print("Enter a replacement string: ");

 String repl;

 repl = input.nextLine();

 

 System.out.println("Your new string is: "+longstring.replace(substring, repl));

 System.out.println("Goodbye!");

}

}

Explanation:

Because of the length of the explanation; I've added the explanation as an attachment where I used comments to explain the lines

View image MrRoyal

In this exercise we have to use the knowledge of the JAVA language to write the code, so we have to:

The code is in the attached photo.

So to make it easier the JAVA code can be found at:

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String longstring, substring;

System.out.print("Enter a long string: ");

longstring = input.nextLine();

System.out.print("Enter a substring: ");

substring = input.nextLine();

System.out.println("Length of your string: "+longstring.length());

System.out.println("Length of your substring: "+substring.length());

System.out.println("Starting position of your substring: "+longstring.indexOf(substring));

String before = longstring.substring(0, longstring.indexOf(substring));

System.out.println("String before your substring: "+before);

String after = longstring.substring(longstring.indexOf(substring) + substring.length());

System.out.println("String after your substring: "+after);

System.out.print("Enter a position between 0 and "+(longstring.length()-1)+": ");

int pos;

pos = input.nextInt();

System.out.println("The character at position "+pos+" is: "+longstring.charAt(pos));

System.out.print("Enter a replacement string: ");

String repl;

repl = input.nextLine();

System.out.println("Your new string is: "+longstring.replace(substring, repl));

System.out.println("Goodbye!");

}

}

See more about JAVA at brainly.com/question/2266606?

View image lhmarianateixeira