Write ONE program containing all these string exercises. Exercise 1 Read a strin
ID: 673755 • Letter: W
Question
Write ONE program containing all these string exercises.
Exercise 1
Read a string from the user into a string named string1.
Test it with the data: Good morning
Print the following heading and the data contained in string1:
Exercise 1
Good morning
Exercise 2
Create a new string named string2 containing the contents from string1.
Append the following text to the end of string 2.
sleepy heads
(Note that there is a space before sleepy.)
Print the following heading and the data contained in string2:
Exercise 2
Good morning sleepy heads
Exercise 3
Create string3 with an initial value of:
bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow
Find the location of the first h in string3
Print the following heading and the location found in string3:
Exercise 3
20
Exercise 4
Create an empty string named string4.
Copy string3 into string4.
Using the location found in exercise 3,
change the first h into J
Print the following heading and the data contained in string4:
Exercise 4
bellow cello fellow Jello mellow Novello Othello pillow Rollo solo yellow
Exercise 5
Create a copy of string3 named string5.
Use the find_last_of member function to find the position of the last l
Print the following heading and the position of the last l
Exercise 5
70
Exercise 6
Create a new string named string6 containing the contents from string3.
Using the position of the last l found in exercise 5, erase all characters from string6 following the last l
Print the following heading and the data contained in string6:
Exercise 6
bellow cello fellow hello mellow Novello Othello pillow Rollo solo yell
Explanation / Answer
import java.util.Scanner;
public class TestString {
public static void main(String ar[]){
String string1;
/*Scanner scanner=new Scanner(System.in);
System.out.print("Enter string1 : ");
string1=scanner.nextLine();*/
string1="Good morning";
//Exercise 1
printString("Exercise 1 "+string1);
//Exercise 2
String string2=string1.concat(" sleepy heads");
printString("Exercise 2 "+string2);
//Exercise 3
String string3="bellow cello fellow hello mellow Novello Othello pillow Rollo solo yellow";
int firstIndex=string3.indexOf("h");
printString("Exercise 3 "+firstIndex);
//Exercise 4
String string4="";
string4=string3.replaceFirst(""+string3.charAt(firstIndex)+"", "J");
printString("Exercise 4 "+string4);
//Exercise 5
String string5=new String(string3);
int lastIndex=string5.lastIndexOf("l");
printString("Exercise 5 "+lastIndex);
//Exercise 6
String string6=new String(string3.substring(0,lastIndex+1));
printString("Exercise 6 "+string6);
}
public static void printString(String string){
System.out.println(string);
}
}
output :
Exercise 1
Good morning
Exercise 2
Good morning sleepy heads
Exercise 3
20
Exercise 4
bellow cello fellow Jello mellow Novello Othello pillow Rollo solo yellow
Exercise 5
70
Exercise 6
bellow cello fellow hello mellow Novello Othello pillow Rollo solo yell
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.