首字母变大写
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 66882 Accepted Submission(s): 36383
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
问题链接:HDU2026 首字母变大写。
问题描述:(略)
问题分析:
这是一个字符串输入处理问题,也是一个基本的输入处理训练问题。
程序说明:
通过这个例子,可以知道C++程序中如何读入一行,如何判定输入结束(文件结束),以及如何用库函数进行字母判定和大小写转换。
如果用C语言来写这个程序,则是另外一番风景,参见后面给出的代码。由于新标准库函数中,gets()不被推荐使用(还可以使用),也可以编写自己的函数mygets()实现。
AC的C++语言程序如下:
/* HDU2026 首字母变大写 */
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string s;
while(getline(cin, s)) {
if(islower(s[0]))
s[0] = toupper(s[0]);
for(int i=1, len=s.length(); i<len; i++) {
if(s[i-1] == ' ' && islower(s[i]))
s[i] = toupper(s[i]);
}
cout << s << endl;
}
return 0;
}
AC的C语言程序如下:
/* HDU2026 首字母变大写 */
#include <stdio.h>
#include <ctype.h>
#define MAXN 100
char s[MAXN+1];
int main(void)
{
int i;
while(gets(s) != NULL) {
i=0;
while(s[i]) {
if(i == 0) {
if(islower(s[i]))
s[i] = toupper(s[i]);
} else {
if(islower(s[i]) && (s[i - 1] == ' ' || s[i - 1] == '\t'))
s[i] = toupper(s[i]);
}
i++;
}
puts(s);
}
return 0;
}
AC的C语言程序如下:
/* HDU2026 首字母变大写 */
#include <stdio.h>
#define MAXN 100
#define DELTA 'a'-'A'
int mygets(char s[])
{
int i = 0;
char c;
while((c = getchar()) && c != '\n' && c != EOF)
s[i++] = c;
s[i] = '\0';
return i > 0 || c != EOF;
}
int main(void)
{
char s[MAXN+1];
int i;
while(mygets(s)) {
if('a' <= s[0] && s[0] <= 'z')
s[0] -= DELTA;
for(i=1; s[i]!='\0'; i++) {
if(s[i-1] == ' ' && 'a' <= s[i] && s[i] <= 'z')
s[i] -= DELTA;
}
printf("%s\n", s);
}
return 0;
}