将所有单词的首字母变为大写

package com.ethjava;

import java.util.Scanner;

public class firstA {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            char[] ch = str.toCharArray();
            for (int i = 0; i < ch.length - 1; i++) {
                //System.out.print(ch[i]);
                if ((ch[0] >= 'a') && (ch[0] <= 'z'))
                    ch[0] = Character.toUpperCase(ch[0]);//对第一个单词的首字母进行处理
                if ((ch[i] == ' ') || (ch[i] == '\t') || (ch[i] == '\r') || (ch[i] == '\n'))
                    ch[i + 1] = Character.toUpperCase(ch[i + 1]);
            }
            for (int i = 0; i < ch.length; i++) {
                System.out.print(ch[i]);
            }

        }


    }
}


题目描述:

你可能感兴趣的:(string,java,java,string)