C++字符串——习题

760. 字符串长度

https://www.acwing.com/problem/content/762/

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int main()
{
    char s[111];
    cin.getline(s,111);
    cout << strlen(s) << endl;
    
    return 0;
}

 AcWing 761. 字符串中的数字个数

https://www.acwing.com/problem/content/763/

#include <bits/stdc++.h>
using namespace std;
int main(){
    int s = 0;
    char n;
    while(cin >> n){
        if(n >= '0' && n <= '9') s++;
    }
    cout << s;
    return 0;
}

 763. 循环相克令

https://www.acwing.com/problem/content/765/

#include<bits/stdc++.h>
using namespace std;
int main (){
    int t;
    cin>>t;
    while(t--){
        string a,b;
        cin>>a>>b;
        if(a==b)puts("Tie");
        else if(a=="Hunter"&&b=="Gun"||a=="Bear"&&b=="Hunter"||a=="Gun"&&b=="Bear")
        puts("Player1");
                else puts("Player2");
    }
    return 0;
}

 765. 字符串加空格

找不到页面 - AcWing

#include<bits/stdc++.h>
using namespace std;
int main ()
{
    string a;
    getline(cin,a);
    for(int i=0;i<a.size();i++)
    cout<<a[i]<<" ";
    
    return 0;
}

769. 替换字符

https://www.acwing.com/problem/content/771/

#include <iostream>
using namespace std;
int main()
{
    string s;//字符串
    cin >> s;
    char c;//待替换字符
    cin >> c;
    for(auto e : s)//范围 for 循环
    {
        if(c == e) //是待替换的字符
            cout <<"#";
        else//不是待替换的字符
            cout<<e;
    }
}

773. 字符串插入

https://www.acwing.com/problem/content/775/

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char str[11], substr[4];

    int n = 2;
    while(scanf("%s %s", str, substr) != EOF){
        int cnt = str[0], res = 0;
        for(int i = 0; i < strlen(str); i++){
            if(str[i] > cnt) cnt = str[i], res = i;
            else if(str[i] == cnt) continue;
        }
        for(int i = 0; i <= res; i ++){
            cout<< str[i];
        }
        for(int i = 0; i < strlen(substr); i++){
            cout<< substr[i];
        }
        for(int i = res + 1; i < strlen(str); i++){
            cout<< str[i];
        }
        puts("");
    }
    return 0;
}

 772. 只出现一次的字符

https://www.acwing.com/problem/content/774/

#include <bits/stdc++.h>
using namespace std;

int st[166];
int a[33];
int main()
{
    string s;
    cin >> s;
    int len = s.size(),flag = 1,j=0;
    for(int i = 0 ; i < len; i++){
        st[(int)s[i]]++;
    }
    for(int i = 90; i < 144; i++){
        if(st[i] == 1){
            a[j] = i;j++;
        }
    }
    if(j == 0) cout << "no";
    else{
    for(int i = 0; i < len; i++){
        for(int h = 0; h < j; h++){
            if(s[i] == (char)a[h]){
                cout << s[i];
                return 0;
            }
        }
    }
    }
    
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永夜天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值