0
点赞
收藏
分享

微信扫一扫

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;
            }
        }
    }
    }
    
}
举报

相关推荐

c++字符串

C++分割字符串

字符串压缩(C++)

C++字符串拼接

C++的字符串

C++字符串详解

c++ 字符串插入

字符串习题1

0 条评论