PAT Grade A-String Processing Type-1035 Password Problem Solving Ideas

1035 Password (20 points)

Insert picture description here

Ideas

Use the find and replace of the string type to kill, without the need to use the cumbersome char array, the
string type is used when determining whether there is an element in the string and replacing an element

The char type array is only used when transforming the array, and remember to put'\0' after it

The specific use of the replace function and find function of the string type are given below. Replace must have a symbol to be replaced before it can be used, otherwise an error will be reported.

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

Code

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

Guess you like

Origin blog.csdn.net/weixin_43999137/article/details/114098504