【PAT】A1035 Password【字符串处理】【水】

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output Specification:

For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.

Sample Input 1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

Sample Output 1:

2
Team000002 RLsp%dfa
Team000001 R@spodfa

Sample Input 2:

1
team110 abcdefg332

Sample Output 2:

There is 1 account and no account is modified

Sample Input 3:

2
team110 abcdefg222
team220 abcdefg333

Sample Output 3:

There are 2 accounts and no account is modified

题目大意

按题目要求进行替换,如果没有需要替换的行按题目要求输出文字提示,否则输出替换行数和对应替换后的行。

思路

由于一行只有两个字段,没有必要使用结构体。使用一个int型数组modified来存放修改过的行号(从0开始),使用M来存储修改的行数(也即是modified数组中有效数据的个数)。

代码


#include <iostream>
#include <algorithm>
#define maxN 1000
#define maxChar 11
using namespace std;
int main() {
    char user[maxN][maxChar], password[maxN][maxChar];
    char origin[] = {'1', '0', 'l', 'O'};
    char replacement[] = {'@', '%', 'L', 'o'};
    int modified[maxN];
    int N, M = 0;
    
    
    scanf("%d", &N);
    for(int i = 0; i < N; i++){
        scanf("%s %s", user[i], password[i]);
        bool flag = false;
        for(int j = 0; password[i][j] != '\0'; j++){
            for(int k = 0; k < 4; k++){
                if(password[i][j] == origin[k]){
                    password[i][j] = replacement[k];
                    flag = true;
                }
            }
        }
        if(flag) modified[M++] = i;
    }
    if(M == 0){
        if(N == 1){
            printf("There is 1 account and no account is modified");
        }else{
            printf("There are %d accounts and no account is modified", N);

        }
    }else{
        printf("%d\n", M);
    }
    for(int i = 0, t; i < M; i++){
        t = modified[i];
        printf("%s %s\n", user[t], password[t]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要替换一个字符串中的某个子串,可以使用C语言中的字符串函数strstr()和strcpy()。具体步骤如下: 1. 使用strstr()函数找到需要替换的子串在原字符串中的位置。 2. 使用strcpy()函数将替换后的新子串复制到一个新的字符串中。 3. 将新字符串和原字符串合并起来,即完成了字符串替换的操作。 下面是一个示例代码,可以实现将字符串中的某个子串替换成另一个字符串: ```c #include <stdio.h> #include <string.h> void replace_str(char *str, char *old, char *new) { char *pos = strstr(str, old); if (pos != NULL) { int len_old = strlen(old); int len_new = strlen(new); if (len_new > len_old) { memmove(pos + len_new, pos + len_old, strlen(pos + len_old) + 1); } memcpy(pos, new, len_new); } } int main() { char str[100] = "pta is good, pta is great!"; replace_str(str, "pta", "PAT"); printf("%s\n", str); return 0; } ``` 输出结果为: ``` PAT is good, PAT is great! ``` ### 回答2: c语言中的字符串替换可以通过使用字符串处理函数和循环来实现。下面是一个用C语言实现字符串替换的示例代码: ```c #include <stdio.h> #include <string.h> void replaceString(char *str, const char *find, const char *replace) { int findLen = strlen(find); int replaceLen = strlen(replace); int strLen = strlen(str); int newSize = strLen; int replaceCount = 0; // 计算新字符串的长度,以便分配足够的内存 for (int i = 0; i < strLen; i++) { if (strstr(&str[i], find) == &str[i]) { newSize -= findLen; newSize += replaceLen; replaceCount++; i += findLen - 1; } } // 分配内存 char *result = (char *)malloc(newSize + 1); memset(result, 0, newSize + 1); int j = 0; for (int i = 0; i < strLen; i++) { if (strstr(&str[i], find) == &str[i]) { strncpy(&result[j], replace, replaceLen); j += replaceLen; i += findLen - 1; } else { result[j++] = str[i]; } } printf("替换前的字符串: %s\n", str); printf("要替换的字符串: %s\n", find); printf("替换后的字符串: %s\n", result); printf("共替换了 %d 处\n", replaceCount); free(result); } int main() { char str[100] = "pta是一个pta网站"; const char find[] = "pta"; const char replace[] = "PAT"; replaceString(str, find, replace); return 0; } ``` 该示例代码中定义了一个`replaceString`函数,并在`main`函数中调用。在`replaceString`函数中,我们首先获取要替换的字符串的长度和原始字符串的长度,然后计算出替换后的字符串的长度。利用循环遍历原始字符串,查找要替换的子字符串,如果找到,则将替换字符串复制到新字符串中;如果未找到,则将原始字符串中的字符复制到新字符串中。 在`main`函数中,我们定义了一个原始字符串`str`,以及要查找和替换的字符串`find`和`replace`。然后调用`replaceString`函数进行字符串替换,并输出替换前的字符串、要替换的字符串、替换后的字符串和替换的次数。 以上就是一个简单的C语言字符串替换的例子。通过使用字符串处理函数和循环,我们可以在C语言中实现字符串替换。 ### 回答3: 在C语言中,我们可以使用字符串函数来实现字符串的替换操作。下面是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> void replaceString(char *str, const char *old_substr, const char *new_substr) { char *pos = strstr(str, old_substr); // 在str中找到第一个出现的old_substr的位置 while (pos != NULL) { size_t old_len = strlen(old_substr); // 获取old_substr的长度 size_t new_len = strlen(new_substr); // 获取new_substr的长度 size_t tail_len = strlen(pos + old_len); // 获取原字符串剩余部分的长度 if (new_len > old_len) { memmove(pos + new_len, pos + old_len, tail_len + 1); // 将原字符串剩余部分向后移动 } else if (new_len < old_len) { memmove(pos + new_len, pos + old_len, tail_len + 1); // 将原字符串剩余部分向前移动 } strncpy(pos, new_substr, new_len); // 将new_substr的内容拷贝到原字符串中 pos = strstr(pos + new_len, old_substr); // 在修改后的字符串中继续查找下一个old_substr的位置 } } int main() { char str[100] = "pta is a good platform. pta helps me improve coding skills."; char old_substr[] = "pta"; char new_substr[] = "PTA"; replaceString(str, old_substr, new_substr); printf("%s\n", str); // 输出修改后的字符串 return 0; } ``` 上述代码中的`replaceString`函数可以将原字符串中的所有`old_substr`替换为`new_substr`,并且保持原字符串的长度不变。在`main`函数中,我们定义了一个字符串`str`,并将其中的所有"pta"替换为"PTA"。最后,我们将修改后的字符串输出到控制台上。 以上代码仅为示例,实际替换操作可能需要根据具体需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值