首字母大写--C++实现

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

题目描述

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

C++实现:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main(){
    char c[100];
    while(gets(c)){
        int num=strlen(c);
        //注意非字母的特殊处理
        if(c[0]>='a'&&c[0]<='z')
            cout<<(char)('A'-'a'+c[0]);
        else
            cout<<c[0];
        for(int i=1;i<num;i++){
            if((c[i-1]==' '||c[i-1]=='\t'||c[i-1]=='\r'||c[i-1]=='\n')&&c[i]>='a'&&c[i]<='z'){
                cout<<(char)('A'-'a'+c[i]);
            }
            else
                cout<<c[i];
        }
        cout<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/zhang__shuang_/article/details/87638833