PAT甲级-字符串处理类型-1035 Password解题思路

1035 Password (20 分)

在这里插入图片描述

思路

使用string类型的find和replace秒杀,而不需要使用繁琐的char数组
string类型在确定字符串内是否有某元素,替换某元素时使用

char类型数组在变换数组时才使用,并且要记得后面置’\0’

下面给出string类型的replace函数和find函数的具体使用,replace必须存在有被替换的符号才可以使用,否则报错。

int main() {
    string a, b, c;
    cin >> a >> b >> c;
    int  len=b.length();
    while (a.find(b) != -1) 
    {
        int i = a.find(b);
        a.replace(i,len,c);
    }
    cout << a << endl;
    return 0;
}

代码

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

int main()
{
    int n;
    scanf("%d",&n);
    string name[1005];
    string strs[1005];

    int num[1005]={0};
    int sum = 0;
    for (int i =0 ;i<n;i++)
    {
        cin>> name[i]>>strs[i];
        if (strs[i].find('1')!=-1 ||strs[i].find('l')!=-1||strs[i].find('0')!=-1||strs[i].find('O')!=-1)
        {
                sum+=1;
                num[i]=1;
        }
        
        while (strs[i].find("1") != -1)
            strs[i] = strs[i].replace(strs[i].find("1"),1, "@");
        while (strs[i].find("l") != -1)
            strs[i] = strs[i].replace(strs[i].find("l"),1, "L");
        while (strs[i].find("0") != -1)
            strs[i] = strs[i].replace(strs[i].find("0"),1, "%");
        while (strs[i].find("O") != -1)
            strs[i] = strs[i].replace(strs[i].find("O"),1, "o");
    }

    if(sum ==0)
        if(n==1)
            printf("There is %d account and no account is modified",n-sum);
        else
            printf("There are %d accounts and no account is modified",n-sum);
    else
    {
        cout<<sum<<endl;
        for (int i =0 ;i<n;i++)
            if(num[i]!=0)
                cout<<name[i]<<' '<<strs[i]<<endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wujiekd

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

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

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

打赏作者

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

抵扣说明:

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

余额充值