题解 | #首字母大写#

首字母大写

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

#include<bits/stdc++.h>
using namespace std;

int main() {
    string str;
    getline(cin, str);//获取输入的字符串

    //遍历字符串
    for (int i = 0; i < str.length(); i++) {
        //如果该字符为字母且位于首位或前一个字符为空白符,则转换为大写
        if (isalpha(str[i]) && (i == 0 || isspace(str[i - 1]))) {
            str[i] = toupper(str[i]);
            cout << str[i];
        } 
        //否则输出原字符
        else {
            cout << str[i];
        }
    }
}

全部评论

相关推荐

06-16 15:04
黑龙江大学 Java
零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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