Acwing 3439. 首字母大写 && 3504. 字符串转换整数

3439. 首字母大写 - AcWing题库

对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。

在字符串中,单词之间通过空格(不一定单个)分隔。

输入格式

一行,一个长度不超过 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>
using namespace std;

int main() {
    string s = "";
    getline(cin, s);
    for(int i = 1; i < s.size(); i++) {
        if(s[i - 1] == ' ')
            s[i] = toupper(s[i]);
    }
    s[0] = toupper(s[0]); 
    cout << s << endl;
    return 0;
}

3504. 字符串转换整数 - AcWing题库

给定一个字符串,字符串由数字和大小写字母构成,请你找到并输出其中的第一个有效整数。

如果无法找到有效整数,或者找到的有效整数超过 int 范围,则输出 −1。

输入格式

一行一个字符串。

输出格式

一个整数表示结果。

扫描二维码关注公众号,回复: 16752755 查看本文章

数据范围

字符串长度不超过 100

输入样例1:

2016

输入样例1:

2016

输入样例2:

o627CSo1

输出样例2:

627

输入样例3:

123456789123abc

输出样例3:

-1

输入样例4:

abc

输出样例4:

-1
#include <iostream>
#include <climits>
using namespace std;

int main() {
    string s = "";
    cin >> s;
    long long res = 0;
    int i = 0, j = 0;
    for(i = 0, j = 0; i < s.size(); i++, j++) {
        while(j < s.size() && isdigit(s[j])) {
            res = res * 10 + s[j] - '0';
            j++;
        }
        if(i != j)  break;
    }
    if(res > INT_MAX || i == j)   cout << -1 << endl;
    else cout << res << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_72758935/article/details/132732114
今日推荐