题解 | #首字母大写#

首字母大写

https://www.nowcoder.com/practice/91f9c70e7b6f4c0ab23744055632467a

利用getline来处理一整行的输入
#include<iostream>
#include<string>
using namespace std;

int main() {
	string str;
	while (getline(cin,str) )
	{
		if (str[0] >= 'a' && str[0] <= 'z') 
			str[0] = str[0] + 'A' - 'a';
		for (int i = 0; i < str.length(); i++) 
		{

			if ((str[i] == ' '||str[i]=='\t'||str[i]=='\r'||str[i]=='\n' )&& (str[i + 1] >= 'a' && str[i + 1] <= 'z')) {
				str[i + 1] += 'A' - 'a';
			}

		}
		cout << str << endl;
	}

}

全部评论

相关推荐

小浪_Coding:6
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务