In: Computer Science
Meant to be written in Java JDK 14.0
4. Define 2 String variables str1 and str2, get input from console and assign input to str1 and str2. (a) Display the length of str1 and str2 (b) Display the first and last characters in str1 and str2 (c) Compare str1 and str2 to check if they are equal or not: (1) The comparison is case sensitive (2) The comparison is case insensitive (d) Compare str1 with str2 to check which string is smaller; display the smaller string first: (1) The comparison is case sensitive (2) The comparison is case insensitive (e) Convert characters in str1 to upper case and display new string (f) Convert characters in str2 to lower case and display new string 2020 (g) Extract the first and last characters in str1, extract the 2nd character from the front and the 2nd character from the end in str2; put these 4 characters together to create a new string and display (h) Extract the first words and the last words from both str1 and str2. Combine these words to create a new string. The words should be separated by white space. Display this new string
import java.util.Scanner;
public class StringOperations {
public static void main(String[] args) {
// Define 2 String variables str1
and str2, get input from console and assign input to str1 and
str2.
String str1,str2;
Scanner sc = new
Scanner(System.in);
System.out.print("Enter string1
:");
str1 = sc.nextLine();
System.out.print("Enter string2
:");
str2 = sc.nextLine();
// (a) Display the length of str1
and str2
System.out.println("string1 length
: "+str1.length());
System.out.println("string2 length
: "+str2.length());
// (b) Display the first and last
characters in str1 and str2
System.out.println("first character
of string1 :"+str1.charAt(0));
System.out.println("last character
of string1 :"+str1.charAt(str1.length()-1));
System.out.println("first character
of string2 :"+str2.charAt(0));
System.out.println("last character
of string2 :"+str2.charAt(str2.length()-1));
// (c) Compare str1 and str2 to
check if they are equal or not:
System.out.println("(1) The
comparison is case sensitive ");
System.out.println(str1.equals(str2)?"Both string1 and string2 are
equal":"Both String1 and string2 are not equal");
// (2) The comparison is case
insensitive
System.out.println("(1) The
comparison is case insensitive ");
System.out.println(str1.equalsIgnoreCase(str2)?"Both string1 and
string2 are equal":"Both String1 and string2 are not equal");
// (e) Convert characters in str1 to
upper case and display new string
System.out.println("string1 in
upper case :"+str1.toUpperCase());
// (f) Convert characters in str2 to
lower case and display new string 2020
System.out.println("string2 in
lower case :"+str2.toLowerCase());
// (g) Extract the first and last
characters in str1, extract the 2nd character from the front and
the 2nd character from the end in str2;
// put these 4 characters together
to create a new string and display
String newString =
str1.charAt(0)+""+str1.charAt(str1.length()-1)+""+str2.charAt(1)+""+str2.charAt(str2.length()-2);
System.out.println("new string
after extract requrired characters : "+newString);
// (h) Extract the first words and
the last words from both str1 and str2.
// Combine these words to create a
new string. The words should be separated by white space. Display
this new string
String str1words[] = str1.split("
");
String str2words[] = str2.split("
");
String newString2 = str1words[0]+"
"+str1words[str1words.length-1]+" "+str2words[0]+"
"+str2words[str2words.length-1];
System.out.println("new string
after extract requrired string : "+newString2);
//// (d) Compare str1 with str2 to
check which string is smaller; display the smaller string
first:
System.out.println("Compare str1
with str2 to check which string is smaller;");
int value =
str1.compareTo(str2);
if(value<0){
System.out.println("smaller string is :"+str1);
}else if(value>0){
System.out.println("smaller string is :"+str2);
}else{
System.out.println("both are equal");
}
}
}