【算法练习】字符串处理 poj2690:首字母大写

题目链接:http://bailian.openjudge.cn/practice/2690

2690:首字母大写

总时间限制: 

1000ms

内存限制: 

65536kB

描述

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

题目理解:输入一行字符串,然后对该行字符串进行相应的处理,

WA的原因是第一个字母是大写的没有处理。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

char str[100];
int main(){
    cin.getline(str,100);
    int len=strlen(str);
    // 一开始没有考虑第一个字符
    if(str[0] && str[0]>='a' && str[0]<='z'){
        str[0]=str[0]-'a'+'A';
    }
    for(int i=0;i<len;i++){
        if(str[i]==' ' || str[i]=='\t' || str[i]=='\n') {
            if(str[i+1]>='a' && str[i+1]<='z'){
                str[i+1]=str[i+1]-'a'+'A'; //空格之后的转换成大写
            }
        }
    }
    cout<<str<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40760678/article/details/100063713
今日推荐