A Problem Solved: Processing a String

In this lesson, we will demonstrate some of the methods in the class String.

We'll cover the following

Problem statement

Read a person’s name, compute its length, and identify the first and last names and the person’s initials. If the name is typed on one line, insist that only one space occur between the two names. However, offer some flexibility by allowing spaces to occur before or after the entire name.

We can solve this problem by using some of the methods presented in the previous lessons to manipulate strings. Such manipulation is called string processing.


Here are the steps, in pseudocode, that our program will take:

Read the name
Remove any leading or trailing spaces

Locate the position of the space between the first and last names
Get the first name
Get the last name
Get the length of the name

Get the first initial
Get the last initial
Form a string of the initials punctuated by periods

Display the name and its length
Display the first and last names
Display the initials


Run the program and observe its output.

Get hands-on with 1200+ tech skills courses.