文章

6

粉丝

41

获赞

0

访问

1.3k

头像
首字母大写 题解:
P1240 北京大学机考题
发布于2024年3月6日 11:35
阅读数 360

//只有80%的正确率,有大佬帮忙看看吗

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void capitalize_words(char *str) {
    int i = 0;
    // 处理第一个字符
    if (islower(str[0])) {
        str[0] = toupper(str[0]);
    }
    while (str[i] != '\0') {
        if (isspace(str[i])) {
            i++; // 跳过空白符
            if (islower(str[i])) {
                str[i] = toupper(str[i]);
            }
        }
        i++;
    }
}

int main() {
    char str[100];
    fgets(str, sizeof(str), stdin);
    capitalize_words(str);
    printf("%s\n", str);
    return 0;
}
 

登录查看完整内容


登录后发布评论

1 条评论
snake
2024年3月6日 11:49

空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。

判断时候不能只判断空格

赞(0)