算法笔记 — 首字母大写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37345402/article/details/83713237

题目链接:http://www.codeup.cn/problem.php?cid=100000580&pid=1

题目描述

对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。
在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。

输入

输入一行:待处理的字符串(长度小于100)。

输出

可能有多组测试数据,对于每组数据,
输出一行:转换后的字符串。

样例输入

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<cstring>
using namespace std;
int main(){
	char a[111];
	while(gets(a)){
		int len=strlen(a);
		for(int i=0;i<len;i++){
			if(a[i]>='a'&&a[i]<='z'){
				if(i==0){
					a[i]=a[i]-'a'+'A';
				}else if(a[i-1]==' '||a[i-1]=='\t'||a[i-1]=='\r'||a[i-1]=='\n'){
					a[i]=a[i]-'a'+'A';
				}
			}
		} 
		for(int i=0;i<len;i++){
			cout<<a[i];
		}
		cout<<endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/83713237