题解 | 首字母大写

首字母大写

https://www.nowcoder.com/practice/91f9c70e7b6f4c0ab23744055632467a

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while(in.hasNextLine()){
            String str = in.nextLine();
            boolean flag = true;
            for(int index = 0;index < str.length();index++){
                char c = str.charAt(index);
                if(flag && c>='a' && c <= 'z'){
                    c = (char)('A' - 'a' + c);
                    flag = false;
                }else{
                    flag = false;
                }
                if(c==' ' || c=='\t' || c=='\r' || c == '\n'){
                    flag = true;
                }
                System.out.print(c);
            }
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务