题解 | #简写单词#

简写单词

https://www.nowcoder.com/practice/0cfa856bf0d649b88f6260d878f35bb4

由题可知,我们需要把每个单词的首字母找出来把它变成大写的形式输出即可,由输入的格式我们可以发现除了第一个单词外,每个空格的后面就是我们要找的字母。由此思路即可轻松写出我们的代码
#include<stdio.h>
#include<string.h>
int main()
{
    int num=0;
    char str[10000],str1[100];
    gets(str);
    int longth=strlen(str);
    for(int i=0;i<longth;i++)
    {
        if(i==0)
        {
            if(str[i]<='z'&&str[i]>='a')
            {
                str1[num]=str[i]-32;
                num++;
            }
            else if(str[i]<='Z'&&str[i]>='A')
            {
                str1[num]=str[i];
                num++;
            }
        }
        else
        {
             if(str[i]==' ')
            {
                
                    if(str[i+1]<='z'&&str[i+1]>='a')
                    {
                        str1[num]=str[i+1]-32;
                        num++;
                    }
                    else if(str[i+1]<='Z'&&str[i+1]>='A')
                    {
                        str1[num]=str[i+1];
                        num++;
                    }
                
             }
        }
    }
    puts(str1);
    return 0;
    
}

全部评论
为什么gets会报错啊
点赞 回复 分享
发布于 2022-08-29 10:44 黑龙江
c99以后gets被弃用了,用fgets代替
点赞 回复 分享
发布于 2025-09-14 08:58 河北

相关推荐

2025-12-05 18:09
已编辑
广东药科大学 后端工程师
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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