试题 算法训练 首字母变大写


资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
  对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(‘ ‘)、制表符(‘\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>
using namespace std;

int main(){
string A;
getline(cin,A);
for(int i=0;i<A.length()-1;i++){
if(A[i]==32||A[i]==44||A[i]==46){
if(A[i+1]>=97&&i!=A.length()){
A[i+1]-=32;
}
}
}
if(A[0]>=97){
A[0]-=32;
}
cout<<A;
return 0;
}

在这里插入图片描述