问题 B: 首字母大写

题目描述

对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。
在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\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.

scanf会识别空格作为一个字符串的结束,,,

#include<cstdio>
#include<cstring>
int main(){
	char b[100];
	while(gets (b)){
		int len=strlen(b), row=0, col=0;
		char a[100][100]={};
		for(int i=0; i<len; i++){
			if(b[i]!=' ' && b[i]!='\t' && b[i]!= '\r' && b[i]!='\n'){
				a[row][col++]=b[i];
			} else{
				a[row++][col]='\0';
				col=0;
			}
		}
		
		for (int i=0; i<=row; i++){
			if(a[i][0]>='a')a[i][0]-=32;
		}
		
		for (int i=0; i<=row; i++){
			printf("%s", a[i]);
			if(i<row) printf(" ");
			else printf ("\n");
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/chuyuan_li/article/details/80977198