1131删除指定字符+1570字符替换+1476字符串编辑+1408合法的变量名+1569字符串分离

**1131 - 删除指定字符
** 来源: 东方博宜oj   oj.czos.cn

*解法一:字符串
#include<bits/stdc++.h>
using namespace std;
string s;
char c;
int main()
{
    cin>>s>>c;
    int p=s.find(c);
    while(p!=-1)
    {
        s.erase(p,1);
        p=s.find(c);
    }
    cout<<s;
    return 0;
}

*解法二:字符数组
#include<bits/stdc++.h>
using namespace std;
char a[1000],b[1000],c;
int main()
{
    cin>>a>>c;
    int k=0;
    for(int i=0;a[i];i++)
    {
        if(a[i]!=c)  b[k++]=a[i];
    }
    b[k]='\0';
    cout<<b;
    return 0;
}
//**1570 - 字符替换
//** 来源: 东方博宜oj   oj.czos.cn

//*解法一:
#include<bits/stdc++.h>
using namespace std;
string s;
char c1,c2;
int main()
{
    cin>>s>>c1>>c2;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]==c1) s[i]=c2;
    }
    cout<<s;
    return 0;
}
//*解法二:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    string w,t;   // w,t虽然是单个字符,但要定义成string,才能用在string中
    int p;
    cin>>s>>w>>t;
    p=s.find(w);
    while(p!=-1)
    {
        s.replace(p,1,t);
        p=s.find(w);
    }
    cout<<s<<endl;
    return 0;
}
**1476 - 字符串编辑
** 来源: 东方博宜oj   oj.czos.cn

*解法一:(有点繁琐)
#include<bits/stdc++.h>
using namespace std;
string s;
char a,b;
string c;
bool flag=false;
int main()
{
    getline(cin,s);
    cin>>a;
    if(a=='D')
    {
        cin>>b;
        int p=s.find(b);
        if(p!=-1)
        {
            flag=true;
            s.erase(p,1);
        }
    }
    else if(a=='R')
    {
        cin>>b>>c;
        int p=s.find(b);
        if(p==-1) flag=false;
        else
        {
            flag=true;
            while(p!=-1)
            {
                s.replace(p,1,c);
                p=s.find(b);
            }
        }
    }
    else
    {
        cin>>b>>c;
        int i;
        for(i=s.size()-1;i>0;i--)
        {
            if(s[i]==b)
            {
                flag=true;
                s.insert(i,c);
                break;
            }
        }
        if(i==0&&s[0]==b)
        {
            flag=true;
            s=c+s;
        }
    }
    if(flag==false) cout<< "Not exist";
    else cout<<s;
    return 0;
}

*解法二:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    char order,c;
    string d;
    int p;
    getline(cin,s);
    cin>>order>>c;
    p=s.find(c);
    if(p==-1)
    {
        cout<< "Not exist"<<endl;
        return 0;
    }
    //删除第一个字符c
    if(order=='D')
    {
        s.erase(p,1);
        cout<<s<<endl;
    }
    else if(order=='I')
    {
        cin>>d;
        p=s.rfind(c);
        s.insert(p,d);
        cout<< s<<endl;  
    }
    else if(order=='R')
    {
        cin>>d;
        while(s.find(c)!=-1)
        {
            p=s.find(c);
            s.replace(p,1,d);
        }
        cout<<s<<endl;
    }
    return 0;
}
**1408 - 合法的变量名
** 来源: 东方博宜oj   oj.czos.cn

#include<bits/stdc++.h>
using namespace std;
string s;
bool flag=true;
int main()
{
    getline(cin,s);
    if(isdigit(s[0])) flag=false;
    if(s=="int"||s=="double"||s=="cin"||s=="cout") flag=false;
    for(int i=0;i<s.size();i++)
    {
        if((isalpha(s[i])||isdigit(s[i])||s[i]=='_')==false)
        {
            flag=false;
            break;
        }
    }
    if(flag) cout<< "yes";
    else cout<< "no";
    return 0;
}
**1569 - 字符串分离
** 来源: 东方博宜oj   oj.czos.cn

*解法一:
#include<bits/stdc++.h>
using namespace std;
string s,mi,ma;
int main()
{
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]>='A'&&s[i]<='Z') ma=s[i]+ma;
        else if(s[i]>='a'&&s[i]<='z') mi=mi+s[i];
    }
    cout<< ma;
    cout<<mi;
    return 0;
}

*解法二:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,a,b;
    cin>>s;
    int p=s.size()-1;
    for(int i=0;i<=p;i++)
    {
        if(islower(s[i])) a+=s[i];
        if(isupper(s[p-i])) b+=s[p-i];
    }
    cout<< b+a;
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值