您的位置:首页 > 编程语言 > C语言/C++

OpenJudge百炼习题解答(C++)--题2690:首字母大写

2016-03-08 23:05 253 查看

题:

总时间限制: 1000ms 内存限制: 65536kB

描述对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。
输入输入一行:待处理的字符串(长度小于80)。
输出输出一行:转换后的字符串。
样例输入:
if so, you already have a google account. you can sign in on the right.


样例输出:
If So, You Already Have A Google Account. You Can Sign In On The Right.


解:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s;
getline(cin,s);
	int InWord=0;
	int h=0;
	while(s[h])
	{
		if(s[h]<='Z'&&s[h]>='A')
		{
			InWord=1;
		}
		if(s[h]<='z'&&s[h]>='a'&&InWord==0)
		{
			s[h]+='A'-'a';
			InWord=1;
		}
		if(InWord==1&&s[h]==' '||InWord==1&&s[h]=='\t'||InWord==1&&s[h]=='\r'||InWord==1&&s[h]=='\n')
		{
			InWord=0;
		}
		h++;
	}
	cout<<s;
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: