首页 > 技术文章 > HDU-2026

Western-Trail 2018-01-29 13:12 原文

首字母变大写
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69837 Accepted Submission(s): 37872

Problem Description
输入一个英文句子,将每个单词的第一个字母改成大写字母。

Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。

Output
请输出按照要求改写后的英文句子。

Sample Input
i like acm
i want to get an accepted

Sample Output
I Like Acm
I Want To Get An Accepted

Author
lcy

Source
C语言程序设计练习(四)

解题思路:

gets()读入字符串,对字符串进行遍历操作,如果当前字符为空格则将下一个字符变为大写,如果i=0,则将第一个字符修改为大写字母。

源代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

char a[120];
int main()
{
    while(gets(a))
    {
        int p = strlen(a);
        for(int i = 0;i < p;i++)
        {
            if(a[i] == ' ')
            {
                int j = i + 1;
                a[j] = a[j] - 32;
            }

            if(i==0)
                a[i] = a[i] - 32;
            cout<<a[i];
        }
        cout<<endl;
    }
    return 0;
}

推荐阅读