练习13.带空格的字符串

版权声明:此文章为作者筱睿_原创文章,转载请附上博客链接 https://blog.csdn.net/qq_43756486/article/details/86761810

题例

输入一行为一个字符串,由小写英文字母和空格组成。

任务是把每个单词的首字母修改成相应的大写字母,其他字符不变,把修改过后的字符串输出。

单词由空格进行分隔。

输入输出样例:1组

#1
样例输入:
hello the world
样例输出:
Hello The World

分析

用字符数组
用gets()函数和puts函数
遍历字符串找到空格
如果空格后面是小写就改为大写(-=32)

代码

#include<iostream>
#include<string.h>
using namespace std;
int main(){
	int i,size;
	char a[101];
	gets(a);
	size=strlen(a);
	if(a[0]!=' '&&a[0]>='a')
		a[0]-=32;
	for(i=0;i<size;++i)
	{
		if(a[i]==' '&&a[i+1]>='a')
		a[i+1]-=32;
	}
	puts(a);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43756486/article/details/86761810
今日推荐