Instructions/Design: Write a program that manipulates two Strings. The program i
ID: 3643949 • Letter: I
Question
Instructions/Design: Write a program that manipulates two Strings. The program inputs two Strings (string1and string2), a character (char1), and an integer (position) to represent a character position within a String. The program will display the following:1. Prompt the user to enter
2 Strings 2.Whether string1 is less, greater or equal to string2
3. The number of characters in string1
4. The character that is in the position of string1 ( create a method named: showChar )
You MUST create the following method: char showChar(String s, int pos) // this is method header The method should accept two arguments: a reference to a String object and an integer. The integer argument is a character position within the String, noting the first character is at position 0. When the method executes, it should display the character at that character position.
Sample Output:
Enter first string: Constance
Enter second string: Conner
Constance is greater than Conner
Number of characters in Constance is 9
Enter position noting first character is at 0: 2
Character at position 2 in Constance is: n
Explanation / Answer
import java.util.Scanner;
public class Random {
public static void main(String args[]) {
String str1;
String str2;
Scanner input = new Scanner(System.in);
System.out.print("Enter first string: ");
str1 = input.nextLine();
System.out.print("Enter second string: ");
str2 = input.nextLine();
if(str1.compareTo(str2) < 0)
System.out.println(str1 + " is less than " + str2);
else if(str1.compareTo(str2) > 0)
System.out.println(str1 + " is greater than " + str2);
else
System.out.println(str1 + " is equal to " + str2);
System.out.println("Number of characters in " + str1 + " is " + str1.length());
System.out.print("Enter position noting first character is at 0: ");
int pos = input.nextInt();
System.out.println("Character at position " + pos + " in " + str1 + " is: " + str1.charAt(pos));
}
}