For this lab you will write a Java program that will make use of a variety of St
ID: 3783071 • Letter: F
Question
For this lab you will write a Java program that will make use of a variety of String methods. Following the instructions from Closed Lab 01, create a new folder named Project02 and a new Java program in that folder named Projecto2.java for this assignment. Project 02 Sample ouptut This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program 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 in string: 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 Your program should work for any arbitrary string and substring. Here's another sample transcript of a different execution of the same code: 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 in string: 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 istExplanation / Answer
package snippet;
import java.util.Scanner;
public class project02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter long string :");
Scanner sc=new Scanner(System.in);
String astr=sc.nextLine();
System.out.println("Enter sub string :");
String substr=sc.next();
System.out.println("Length of string :"+astr.length());
System.out.println("Length of sub string :"+substr.length());
int index=astr.indexOf(substr);
String retu=astr.substring(index);
System.out.println(retu);
System.out.println("Starting position of your substring in string "+index);
System.out.println("String before your substring "+astr.substring(0, index-1));
System.out.println("String after your substring "+astr.substring(index+substr.length(),astr.length()));
System.out.println("Enter position between 0 and "+(astr.length()-1));
int i=sc.nextInt();
System.out.println("Chatacter at position "+i+" is "+astr.charAt(i));
System.out.println("enter a replacement string ");
String rep=sc.next();
System.out.println("Your nes string is "+astr.replaceAll(substr, rep));
}
}
==========================================
Enter long string :
akshay is smart
Enter sub string :
smart
Length of string :15
Length of sub string :5
smart
Starting position of your substring in string 10
String before your substring akshay is
String after your substring
Enter position between 0 and 14
5
Chatacter at position 5 is y
enter a replacement string
dumb
Your nes string is akshay is dumb
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.