This exercise asks you to write a program that tests some of the built-in subroutines for working with Strings. The program should ask the user to enter their first name and their last name, separated by a space. Read the user's response using TextIO.getln(). Break the input string up into two strings, one containing the first name and one containing the last name. You can do that by using the indexOf() subroutine to find the position of the space, and then using substring() to extract each of the two names. Also output the number of characters in each name, and output the user's initials. (The initials are the first letter of the first name together with the first letter of the last name.) A sample run of the program should look something like this:

Please enter your first name and last name, separated by a space.
? Mary Smith
Your first name is Mary, which has 4 characters
Your last name is Smith, which has 5 characters
Your initials are MS

Respuesta :

Answer:

Java.

Explanation:

// Get user input

System.out.print("Please enter your first name and last name separated by a space: ");

userInput = TextIO.getln();

// Find index of space character

int spaceIndex = userInput.indexOf(' ');

// Extract first and last name using space character index

// I have used length() method to get length of string userInput.

String firstName = userInput.substring(0, spaceIndex-1);

String lastName = userInput.substring(spaceIndex+1, userInput.length()-1);

// Print the required statements

System.out.print("Your first Name is %s, which has %d characters%n", firstName, firstName.length());

System.out.print("Your last Name is %s, which has %d characters%n", lastName, lastName.length());

// I have used space character Index to get the Initial of last name

System.out.print("Your initials are %s%s", firstName.substring(0,0), lastName.substring(spaceIndex+1, spaceIndex+1));