Question:
Write a program that will take a string of someone's full name, and break it into separate strings of first name and last name, as well as capitalize the first letter of each. Again, use pointers and string manipulation functions only.
Example: "joseph smith" "Joseph" "Smith"
Code:
import java.util.Scanner; public class Example { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter name: "); String name = sc.nextLine(); String first = name.substring(0, name.indexOf(" ")); String last = name.substring(name.indexOf(" ") + 1); String cap = Character.toString(first.charAt(0)).toUpperCase(); first = cap + first.substring(1); cap = Character.toString(last.charAt(0)).toUpperCase(); last = cap + last.substring(1); System.out.println(name); System.out.println(first); System.out.println(last); } }
Output:
$ java Example Enter name: joseph smith joseph smith Joseph Smith