Question

In: Computer Science

Many documents use a specific format for a person's name. Write a program whose input is:...

Many documents use a specific format for a person's name. Write a program whose input is:

firstName middleName lastName

and whose output is:

lastName, firstInitial.middleInitial.

If the input has the form:

firstName lastName

the output is:

lastName, firstInitial.

Ex: If the input is:

user_input = input()
splits = user_input.split(" ")
if(len(splits) == 3):
first_name = splits[0]
middle_name = splits[1]
last_name = splits[2]
print(last_name + ", " + first_name[0] + "." + middle_name[0] + ".")
elif(len(splits) == 2):
first_name = splits[0]
middle_name = splits[1]
print(middle_name + ", " + first_name[0] + ".")
  

I got helped on this, but someone explain to me line by line what it means? Splits == 3, how do I know it's three?! the [0] and [1] why does first name have[0] what does the 0, 1 mean?!!

Solutions

Expert Solution

Program: In this program, we are taking input from the user, and then after the processing the string we output the string in specific format.

Steps:

  • First we split the string based on the spaces as regex using the split function and store it in a list called splits
  • The size of our list depends on the input given by the user, if the user input included first name, middle name and last name, then our size of list would be three - ["firstName","middleName","lastName"], which is checked by the if condition - if(len(splits) == 3), here "len" is an inbuilt function which returns size of array/list. If the user input included only first name and last name, the size of list would be two - ["firstName","lastName"].
  • Then we store the firstname, middleName (if present), lastName in different variables. As we want to print the full last name, but only initials of firstName and middleName, so to print the first character of the string first_name we have to print the character present at index 0 index which would be written as first_name[0] and similarly for middle name middle_name[0].
  • Print the result

We can access any character of a string using its indices, for e.g - fruit = "apple", if we print fruit[0] we get 'a' as output, if we print fruit[1] we get 'p' as output and similarly if we print fruit[4] we get 'e' as output.

Below is the code with comments for more visualisation:

Code:

user_input = input()

# The slit() function splits the string input based on the regex passed in it
# and returns a list of strings, here we are storing it in a variable named splits
# Here we passed space as our regex and hence our input would be split based on the spaces in the string input
# for e.g - input => Mary Elizabeth Smith
# splits = ["Mary", "Elizabeth", "Smith"]
splits = user_input.split(" ")

# If user input was - firstName middleName lastName
# splits would have a list of size 3 like this ["firstName","middleName","LastName"]
# at index 0 we have first name, at index 1 middle name, and at index 2 last name
if(len(splits) == 3):
    
    # Store firstName, first element of splits list
    first_name = splits[0]
    
    # Store middleName, second element of splits list
    middle_name = splits[1]
    
    # Store lastName, third element of splits list
    last_name = splits[2]
    
    # As we want to access the first characters of first name and middle name
    # the first character is stored at 0th index, first_name[0] would give first character of first name
    # and middle_name[0] would give first character of middle name
    
    # Print lastName, firstInitial.middleInitial.
    print(last_name + ", " + first_name[0] + "." + middle_name[0] + ".")
    
# else if the input was - firstName lastName
# splits would have a list of size 2 like this ["firstName","lastName"]
# at index 0 we have first name, at index 1 last name
elif(len(splits) == 2):
    
    #Store firstName, first element of splits list
    first_name = splits[0]
    
    #Store lastName, second element of splits list
    middle_name = splits[1]
    
    # As we want to access the first character of first name and middle name
    # the first character is stored at 0th index, first_name[0] would give first character of first name
    
    #Print lastName, firstInitial.
    print(middle_name + ", " + first_name[0] + ".")
  

Code image:

Output 1:

Output 2:

# Please ask for any doubts. Thanks.


Related Solutions

Write a program whose input is two integers, and whose output is the first integer and...
Write a program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. import java.util.Scanner; public class LabProgram {...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 The output is: 8 3 Your program must define and call the following function. swap_values() returns the two values in swapped order. def swap_values(user_val1, user_val2) **in Python, please
Write a C++ program to maintain a fixed size library of 1024 documents, whose size randomly...
Write a C++ program to maintain a fixed size library of 1024 documents, whose size randomly ranges between 2MB to 3MB.
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT