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

猜你喜欢

转载自blog.csdn.net/weixin_43999137/article/details/114098504