将原密码中的字母和特殊符号取出,组成一个新的字符串,该字符串每个字符前需增加该字符在原密码中的位置信息。(举例:输入密码SC1a2***,则新字符串为“1S2C4a678*”)。将新字符串中的大写字母转换成小写字母,并循环后移3个位置。(如A转换为d,Z转换为c);将小写字母转换成大写字母,并循环前移5个位置,形成一个新的字符串2。用c++编写
时间: 2023-05-28 11:05:19 浏览: 155
#include <stdio.h>
#include <string.h>
void shift(char *str, int n) { // 循环左移
int len = strlen(str);
n %= len;
char temp;
for (int i = 0; i < n; i++) {
temp = str[0];
for (int j = 0; j < len - 1; j++) {
str[j] = str[j + 1];
}
str[len - 1] = temp;
}
}
int main() {
char password[100];
printf("请输入密码:");
scanf("%s", password);
char new_str[100] = "";
int j = 0;
for (int i = 0; i < strlen(password); i++) {
if ((password[i] >= 'a' && password[i] <= 'z') || (password[i] >= 'A' && password[i] <= 'Z') || (password[i] >= '0' && password[i] <= '9') || (password[i] == '*' || password[i] == '/' || password[i] == '+' || password[i] == '-' || password[i] == '=')) {
new_str[j] = password[i];
j++;
new_str[j] = i + 1 + '0';
j++;
}
}
for (int i = 0; i < strlen(new_str); i++) { // 大小写字母转换
if (new_str[i] >= 'a' && new_str[i] <= 'z') {
new_str[i] = ((new_str[i] - 'a' + 3) % 26) + 'a';
} else if (new_str[i] >= 'A' && new_str[i] <= 'Z') {
new_str[i] = ((new_str[i] - 'A' - 5 + 26) % 26) + 'A';
}
}
shift(new_str, 3); // 循环左移3位
printf("新字符串1:%s\n", new_str);
char new_str2[100] = "";
for (int i = 0; i < strlen(new_str); i++) { // 大小写字母转换
if (new_str[i] >= 'a' && new_str[i] <= 'z') {
new_str2[i] = new_str[i] - 32 - 5;
if (new_str2[i] < 'A') {
new_str2[i] += 26;
}
} else if (new_str[i] >= 'A' && new_str[i] <= 'Z') {
new_str2[i] = new_str[i] + 32 - 3;
if (new_str2[i] < 'a') {
new_str2[i] += 26;
}
}
}
printf("新字符串2:%s\n", new_str2);
return 0;
}
阅读全文
相关推荐


















