Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TitleCase {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a string: ");
- String input = scan.nextLine();
- System.out.println(capitalizeEachString(input));
- }
- public static String capitalizeEachString(String str) {
- if (str == null || str.length() == 0) {
- return "";
- }
- if (str.length() == 1) {
- return str.toUpperCase();
- }
- String[] words = str.split(" ");
- StringBuilder newWord = new StringBuilder(str.length());
- for (String word : words) {
- if (word.length() > 1) {
- newWord.append(word.substring(0,1).toUpperCase());
- newWord.append(word.substring(1));
- } else {
- newWord.append(word.toUpperCase());
- }
- newWord.append(" ");
- }
- return newWord.toString().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement