字符串问题

1、第一个只出现一次的字符:在字符串中找出第一个只出现一次的字符。如输入“abaccdeff”则输出b

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

#define length(a) ( (sizeof(a)) / (sizeof(a[0])) )

int main()
{
    string str;
    cin >> str;
    int len = str.size();

    unsigned int ch[256] = { 0 };
    for (int i = 0; i < len; i++) {
        ch[str[i]]++;
    }

    int j = 0;
    while (len--) {
        if (ch[str[j]] == 1) {
            cout << str[j] << endl;
            break;
        }
        j++;
    }
    return 0;
}

2、翻转单词顺序VS左旋转字符串:题目一:输入一个英语句子,翻转句子中单词的顺序,但是单词的字符的顺序不变。变电符号和普通字符一样处理。

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>

using namespace std;

#define length(a) ( (sizeof(a)) / (sizeof(a[0])) )

int main()
{
    vector<string> result;
    vector<int> pos;
    string str;
//  cin >> str;
    // 输入整行
    getline(cin, str);
    int len = str.size();

    // 存储空格所在位置
    pos.push_back(0);
    for (int i = 0; i < len; i++) {
        if (str[i] == ' ')
            pos.push_back(i);
    }
    pos.push_back(len);

    for (int i = pos.size(); i > 2; i--) {
        //cout << pos[i - 1] << " ";
        cout << str.substr(pos[i - 2] + 1, pos[i - 1] - pos[i - 2] -1) << " ";
    }
    cout << str.substr(0, pos[1]);

    cout << endl;
    cout << "length of pos = " << pos.size() << endl;

    return 0;
}

运行结果

3、编码实现求给定字符串(全为小写英文字母)的最小后继,如”abc”的最小后继为”abd”,”dhz”的最小后继为”di”。(Google笔试题)

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>

using namespace std;

#define length(a) ( (sizeof(a)) / (sizeof(a[0])) )

int main()
{
    string str1;
    string str2;
    cin >> str1;

    for (int i = str1.size() - 1; i >= 0; i--) {
        if (str1[i] + 1 <= 'z') {
            str1[i] += 1;
            str1[i + 1] = '\0';
            break;
        }
    }
    cout << str1 << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值