首页 > 试题广场 >

字符串加解密

[编程题]字符串加解密
  • 热度指数:279284 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}规定这样一种密码的加密方法:
\hspace{23pt}\bullet\,对于密码中的英文字母,按照字母表顺序,向后移动一位,同时改变大小写,即 \mathtt{Z} 转换为 \mathtt{a}\mathtt{A} 转换为 \mathtt{b}\mathtt{B} 转换为 \mathtt{c}\cdots\mathtt{Y} 转换为 \mathtt{z}\mathtt{Z} 转换为 \mathtt{a}
\hspace{23pt}\bullet\,对于密码中的数字,增加 19 转换为 0
\hspace{15pt}字符串的解密方法即为加密方法的逆过程。

\hspace{15pt}现在,对于给定的明文字符串 s ,将其加密;对于给定的密文字符串 t ,将其解密。

输入描述:
\hspace{15pt}第一行输入一个长度为 1 \leqq {\rm length}(s) \leqq 10^3 的字符串 s ,代表给定的明文字符串;
\hspace{15pt}第二行输入一个长度为 1 \leqq {\rm length}(t) \leqq 10^3 的字符串 t ,代表给定的密文字符串。

\hspace{15pt}除此之外,保证字符串 st 中仅包含英文字母和数字。


输出描述:
\hspace{15pt}第一行输出一个字符串,代表加密后的 s
\hspace{15pt}第二行输出一个字符串,代表解密后的 t
示例1

输入

abcdefg1
0BCDEFGH

输出

BCDEFGH2
9abcdefg
#include <stdio.h>
#include <ctype.h>

int main()
{
    char st[2002] = {0}; // 为中间的\0和最后的空格预留2个位置
    char ch;
    int i = 0;

    while ((ch = getchar()) != '\n') // 加密
    {
        if (isalpha(ch))
        {
            if (isupper(ch))
            {
                st[i++] = ch == 'Z' ? 'a' : ch + ('a' - 'A') + 1;
            }
            else
            {
                st[i++] = ch == 'z' ? 'A' : ch - ('a' - 'A') + 1;
            }
        }
        else
        {
            st[i++] = ch == '9' ? '0' : ch + 1;
        }
    }
    st[i++] = 0;
    while ((ch = getchar()) != '\n') // 解密
    {
        if (isalpha(ch))
        {
            if (isupper(ch))
            {
                st[i++] = ch == 'A' ? 'z' : ch + ('a' - 'A') - 1;
            }
            else
            {
                st[i++] = ch == 'a' ? 'Z' : ch - ('a' - 'A') - 1;
            }
        }
        else
        {
            st[i++] = ch == '0' ? '9' : ch - 1;
        }
    }
    st[i] = ' ';
    i = -1;
    while (st[++i] != ' ')
    {
        if (st[i] == '\0')
        {
            printf("\n");
            continue;
        }
        printf("%c", st[i]);
    }
    return 0;
}

发表于 2025-02-13 12:12:07 回复(0)
#include <stdio.h>

void encode(char* str) {
    for (int i = 0; i < strlen(str); i ++) {
        if (str[i] >= '0' && str[i] <= '9') {
            str[i] = (str[i] - '0' + 1) % 10 + '0';
        } else if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = (str[i] - 'a' + 1) % 26 + 'A';
        } else if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = (str[i] - 'A' + 1) % 26 + 'a';
        }
    }
}

void deencode(char* str) {
    for (int i = 0; i < strlen(str); i ++) {
        if (str[i] >= '0' && str[i] <= '9') {
            str[i] = (str[i] - '0' - 1 + 10) % 10 + '0';
        } else if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = (str[i] - 'a' - 1 + 26) % 26 + 'A';
        } else if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = (str[i] - 'A' - 1 + 26) % 26 + 'a';
        }
    }
}

int main() {
    char s[1001];
    char t[1001];
    scanf("%s %s", s, t);

    // 加密
    encode(s);
    // 解密
    deencode(t);

    printf("%s\n%s\n", s, t);

    return 0;
}
发表于 2025-02-07 15:48:32 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void encode(char* str, _Bool encode) {
    int i;
    if (encode == 1) {
        for (i = 0; i < strlen(str); i++) {
            if (islower(str[i])) {
                str[i] = str[i] - 'a' + 'A' + 1;
                if (str[i] > 'Z')
                    str[i] = 'A';
            } else if (isupper(str[i])) {
                str[i] = str[i] + 'a' - 'A' + 1;
                if (str[i] > 'z')
                    str[i] = 'a';
            } else if (isdigit(str[i])) {
                str[i] = str[i] + 1;
                if (str[i] > '9')
                    str[i] = '0';
            }
        }
        for (i = 0; i < strlen(str); i++)
            putchar(str[i]);
        printf("\n");
    } else if (encode == 0) {
        for (i = 0; i < strlen(str); i++) {
            if (islower(str[i])) {
                str[i] = str[i] - 'a' + 'A' - 1;
                if (str[i] < 'A')
                    str[i] = 'Z';
            } else if (isupper(str[i])) {
                str[i] = str[i] + 'a' - 'A' - 1;
                if (str[i] < 'a')
                    str[i] = 'z';
            } else if (isdigit(str[i])) {
                str[i] = str[i] - 1;
                if (str[i] < '0')
                    str[i] = '9';
            }
        }
        for (i = 0; i < strlen(str); i++)
            putchar(str[i]);
        printf("\n");
    }
}

int main() {
    char instr[1001];
    char outstr[1001];
    while (scanf("%s %s", instr, outstr) != EOF) { // 注意 while 处理多个 case
        encode(instr, 1);
        encode(outstr, 0);
    }
    return 0;
}

发表于 2024-06-23 11:15:27 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char str0[1001] = {'\0'},str1[1001] = {'\0'};
    gets(str0);
    gets(str1);
    for(int i = 0 ; i < strlen(str0) ; i++){
        if(str0[i] >= 'a' && str0[i] <= 'z'){
            if(str0[i] == 'z')  str0[i] = 'A'; 
            else    str0[i] = str0[i]-31;    
        }else if (str0[i] >= 'A' && str0[i] <= 'Z') {
            if(str0[i] == 'Z')  str0[i] = 'a'; 
            else    str0[i] = str0[i]+33;
        }else if (str0[i] >= '0' && str0[i] <= '9') {
            if(str0[i] == '9')  str0[i] = '0'; 
            else    str0[i] = str0[i]+1;
        }
    }
    printf("%s\n",str0);

    for(int i = 0 ; i < strlen(str1) ; i++){
        if(str1[i] >= 'a' && str1[i] <= 'z'){
            if(str1[i] == 'a')  str1[i] = 'Z'; 
            else    str1[i] = str1[i]-33;    
        }else if (str1[i] >= 'A' && str1[i] <= 'Z') {
            if(str1[i] == 'A')  str1[i] = 'z'; 
            else    str1[i] = str1[i]+31;
        }else if (str1[i] >= '0' && str1[i] <= '9') {
            if(str1[i] == '0')  str1[i] = '9'; 
            else    str1[i] = str1[i]-1;
        }
    }
    printf("%s\n",str1);
    return 0;
}

发表于 2023-12-23 20:05:24 回复(0)
#include <stdio.h>
#include "string.h"

#define max_lenth 1001

void jiami_func(int lenth1, char a[lenth1])
{
    for(int i = 0; i < lenth1; i++)
    {
        if(a[i] >= 'A' && a[i] <= 'Z'){
            if(a[i] == 'Z'){a[i] = 'a';}
            else a[i] += 33;
        }
        else if(a[i] >= 'a' && a[i] <= 'z'){
            if(a[i] == 'z'){a[i] = 'A';}
           else  a[i] -= 31;
        }
        else if(a[i] >= '0' && a[i] <= '9')
        {
            if(a[i] == '9'){a[i] = '0';}
            else a[i] += 1;
        }
        printf("%c",a[i]);
    }
    return ;
}

void jiemi_func(int lenth2, char b[lenth2])
{
    for(int i = 0; i < lenth2; i++)
    {
        if(b[i] >= 'A' && b[i] <= 'Z'){
            if(b[i] == 'A'){b[i] = 'z';}
            else b[i] += 31;
        }
        else if(b[i] >= 'a' && b[i] <= 'z'){
            if(b[i] == 'a'){b[i] = 'Z';}
           else  b[i] -= 33;
        }
         else if(b[i] >= '0' && b[i] <= '9')
        {
            if(b[i] == '0'){b[i] = '9';}
            else b[i] -= 1;
        }
        printf("%c",b[i]);
    }
    return ;
}

int main() {
    char jiami[max_lenth] = {0};
    char jiemi[max_lenth] = {0};
    scanf("%s\n%s", jiami, jiemi);
    int len1, len2;
    len1 = strlen(jiami);
    len2 = strlen(jiemi);
//     printf("%d",len1);
    jiami_func(len1,jiami);
    printf("\n");
    jiemi_func(len2,jiemi);
    return 0;
}
发表于 2023-11-17 15:57:40 回复(0)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//加密
void encryption(char* str,int len)
{
    for(int i = 0;i<len;i++)
    {
        if(islower(str[i]))
        {
            if(str[i] == 'z')
            {
                str[i] = 'A';
            }else  {
            str[i] = toupper(str[i])+1;
            }
        }
        else if(isupper(str[i]))
        {
            if(str[i] == 'Z')
            {
                str[i] = 'a';
            }
            else
            {
                str[i] = tolower(str[i])+1;
            }
        }
        else {
        if(str[i] == '9')
        {
            str[i]='0';
        }
        else {
        str[i] += 1;
        }
        }
    }
}
//解密
void decipher(char* str,int len)
{
    for(int i = 0;i<len;i++)
    {
        if(islower(str[i]))
        {
            if(str[i] == 'a')
            {
                str[i] = 'Z';
            }else  {
            str[i] = toupper(str[i])-1;
            }
        }
        else if(isupper(str[i]))
        {
            if(str[i] == 'A')
            {
                str[i] = 'z';
            }
            else
            {
                str[i] = tolower(str[i])-1;
            }
        }
        else {
        if(str[i] == '0')
        {
            str[i]='9';
        }
        else {
        str[i] -= 1;
        }
        }
    }
}

int main() {
    char password[2][1001] = {'\0'};

    while (scanf("%s", password[0]) != EOF) {
        scanf("%s", password[1]);
        encryption(password[0], strlen(password[0]));
        decipher(password[1], strlen(password[1]));
        printf("%s\n",password[0]);
        printf("%s\n",password[1]);
    }
    return 0;
}
发表于 2023-10-10 11:00:48 回复(0)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define N 1001

void Lock(char s[], int len)
{
    for(int i = 0; i < len; i++){
        if(s[i] >= 'A' && s[i] < 'Z'){
            printf("%c", s[i]+33);
        }else if(s[i] == 'Z'){
            printf("%c", 'a');
        }else if(s[i] >= 'a' && s[i] < 'z'){
            printf("%c", s[i]-31);
        }else if(s[i] == 'z'){
            printf("%c", 'A');
        }else if(s[i] >= '0' && s[i] < '9'){
            printf("%c", s[i]+1);
        }else if(s[i] == '9'){
            printf("%c", '0');
        }else{
            printf("%c", s[i]);
        }
    }
    printf("\n");
}

void Unlock(char s[], int len)
{
    for(int i = 0; i < len; i++){
        if(s[i] > 'A' && s[i] <= 'Z'){
            printf("%c", s[i]+31);
        }else if(s[i] == 'A'){
            printf("%c", 'z');
        }else if(s[i] > 'a' && s[i] <= 'z'){
            printf("%c", s[i]-33);
        }else if(s[i] == 'a'){
            printf("%c", 'Z');
        }else if(s[i] > '0' && s[i] <= '9'){
            printf("%c", s[i]-1);
        }else if(s[i] == '0'){
            printf("%c", '9');
        }else{
            printf("%c", s[i]);
        }
    }
    printf("\n");
}

int main()
{
    char unpasswd[N],passwd[N];
    scanf("%s",unpasswd);
    scanf("%s",passwd);
    Lock(unpasswd, strlen(unpasswd));
    Unlock(passwd, strlen(passwd));
    return 0;
}

发表于 2023-10-05 15:43:17 回复(0)
#include <stdio.h>
#include<string.h>
int main() {
char s1[1001],s2[1001];
int i,j;
gets(s1);
gets(s2);

//加密
for(i=0;s1[i]!='\0';i++)
{
    if(s1[i]>='0'&&s1[i]<='8'||s1[i]>='A'&&s1[i]<='Y'||s1[i]>='a'&&s1[i]<='y')
        s1[i]+=1;    
    else if(s1[i]=='Z'||s1[i]=='z')
        s1[i]-=25;
    else if(s1[i]=='9')
        s1[i]='0';
    if(s1[i]>='A'&&s1[i]<='Z')
    {
        s1[i]+=32;
        continue;
    }
    else if(s1[i]>='a'&&s1[i]<='z')
    {
        s1[i]-=32;
        continue;
    }
}

//解密
for(i=0;s2[i]!='\0';i++)
{
    if(s2[i]>='1'&&s2[i]<='9'||s2[i]>='B'&&s2[i]<='Z'||s2[i]>='b'&&s2[i]<='z')
        s2[i]-=1;    
    else if(s2[i]=='A'||s2[i]=='a')
        s2[i]+=25;
    else if(s2[i]=='0')
        s2[i]='9';
    if(s2[i]>='A'&&s2[i]<='Z')
    {
        s2[i]+=32;
        continue;
    }
    else if(s2[i]>='a'&&s2[i]<='z')
    {
        s2[i]-=32;
        continue;
    }
}

puts(s1);
puts(s2);
return 0;
}
发表于 2023-07-30 22:17:42 回复(0)
#include <stdio.h>
#include <string.h>

char trans(char a){
    if(a<='9'&&a>='0'){
        int b=a-'0';
        b=(b+1)%10;
        a=b+'0';
    }
    else {
    if (a=='z') a='A';
    else if (a=='Z') a='a';
    else if (a<='z'&&a>='a') a=a+1-'a'+'A';
    else a=a+1+'a'-'A';
    }
    return a;
}

char detrans(char a){
    if(a<='9'&&a>='0'){
        int b=a-'0';
        b=(b+9)%10;
        a=b+'0';
    }
    else {
    if (a=='A') a='z';
    else if (a=='a') a='Z';
    else if (a<='z'&&a>='a') a=a-1-'a'+'A';
    else a=a-1+'a'-'A';
    }
    return a;
}

int main() {
    char a1[1001],a2[1001];
    scanf("%s",a1);
    scanf("%s",a2);
    for(int i=0; i<strlen(a1); i++){
        a1[i]=trans(a1[i]);
    }
    for(int i=0; i<strlen(a2); i++){
        a2[i]=detrans(a2[i]);
    }
   printf("%s\n%s",a1,a2);
    return 0;
}

发表于 2023-06-05 15:37:27 回复(0)
#include <stdio.h>
#include <string.h>

void encode(char* a) {
    int len = strlen(a);
    for (int i = 0; i < len; i++) {
        if (a[i] == 'Z') {
            a[i] = 'a';
        } else if (a[i] == 'z') {
            a[i] = 'A';
        } else if (a[i] >= 'a' && a[i] < 'z') {
            a[i] += 'A' - 'a' + 1;
        } else if (a[i] >= 'A' && a[i] < 'Z') {
            a[i] += 'a' - 'A' + 1;
        } else if (a[i] == '9') {
            a[i] = '0';
        } else if (a[i] >= '0' && a[i] < '9') {
            a[i] += 1;
        }
    }
}

void decode(char* b) {
    int len = strlen(b);
    for (int i = 0; i < len; i++) {
        if (b[i] == 'a') {
            b[i] = 'Z';
        } else if (b[i] == 'A') {
            b[i] = 'z';
        } else if (b[i] > 'a' && b[i] <= 'z') {
            b[i] += 'B' - 'b' - 1;
        } else if (b[i] > 'A' && b[i] <= 'Z') {
            b[i] += 'b' - 'B' - 1;
        } else if (b[i] == '0') {
            b[i] = '9';
        } else if (b[i] > '0' && b[i] <= '9') {
            b[i] -= 1;
        }
    }
}

int main() {
    char a[1001] = {};
    char b[1001] = {};
    scanf("%s", a);
    scanf("%s", b);

    encode(a);
    decode(b);

    printf("%s\n", a);
    printf("%s", b);
    return 0;
}

发表于 2023-02-25 18:14:50 回复(0)
//简单题没啥好说得
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    char str1[1024] = {'\0'};
    char str2[1024] = {'\0'};
    char str11[1024] = {'\0'};
    char str22[1024] = {'\0'};
    int i = 0,j = 0;
    int len1 = 0, len2 = 0;
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        len1 = strlen(str1);
        len2 = strlen(str2);
        for(i = 0;i<len1;i++)
        {
            if(str1[i] >= 'a' && str1[i] <= 'z')
            {
                if(str1[i] == 'z')
                {
                  str11[j++]=toupper('a');   
                }
                else
                {
                  str11[j++]=toupper((str1[i]+1)%('z'+1));  
                }
            }
            else if(str1[i] >= 'A' && str1[i] <= 'Z')
            {
                if(str1[i] == 'Z')
                {
                    str11[j++]=tolower('A');   
                }
                else
                {
                    str11[j++]=tolower((str1[i]+1)%('Z'+1));
                }
            }
            else if(str1[i] >= '0' && str1[i] <= '9')
            {
                if(str1[i] == '9')
                {
                    str11[j++]='0';   
                }
                else
                {
                    str11[j++]=(str1[i]+1)%('9'+1);
                }
            }
            else
            {
                str11[j++] = str1[i];
            }
        }
        j = 0;
        for(i = 0;i<len2;i++)
        {
            if(str2[i] >= 'a' && str2[i] <= 'z')
            {
                if(str2[i] == 'a')
                {
                    str22[j++] = toupper('z');
                }
                else
                {
                    str22[j++]=toupper(str2[i]-1);
                }
            }
            else if(str2[i] >= 'A' && str2[i] <= 'Z')
            {
                if(str2[i] == 'A')
                {
                    str22[j++] = tolower('Z');
                }
                else
                {
                    str22[j++]=tolower(str2[i]-1);
                }
            }
            else if(str2[i] >= '0' && str2[i] <= '9')
            {
                if(str2[i] == '0')
                {
                    str22[j++] = '9';
                }
                else
                str22[j++]=str2[i]-1;
            }
            else
            {
                str22[j++] = str2[i];
            }
        }  
        printf("%s\n",str11);
        printf("%s\n",str22);
    }
    return 0;
}
发表于 2022-07-04 00:43:44 回复(0)
简单粗暴,但能用
//查表法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//加密函数:直接用两个密码表,对好位置,检查一下
//输入一个字符,检查原码表,找到输入字符对应的下标
//将密码表对应下标的字符赋值给输入字符
void jiami(char* input){
    char yuan[63]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"};
      char mi[63]={"bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890"};
//    printf("%c",*input);
    for(int i=0;i<63;i++){
        if(yuan[i]==*input){
            *input=mi[i];
            return;
        } 
    } 
}

//解密函数,原理同上
void jiemi(char* input){
    char yuan[63]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"};
      char mi[63]={"bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890"};
//    printf("%c",*input);
    for(int i=0;i<63;i++){
        if(mi[i]==*input){
            *input=yuan[i];
            return;
        } 
    } 
}
//如果萌新不习惯用指针的话,使用char型函数返回密码值,然后在主函数进行赋值也是可以的
//也就是 char jiami(char input)
//主函数中
//    for(int i=0;i<len;i++){
        // char temp=jiami(ch[i]);
        // ch[i]=temp;
//    }

int main(){     //加密第一行字符     char ch[1002];     gets(ch);     int len=strlen(ch);          for(int i=0;i<len;i++){         jiami(&ch[i]);     }     printf("%s\n",ch);//注意换行          //解密第二行字符     char ch2[1002];     gets(ch2);     len=strlen(ch2);          for(int i=0;i<len;i++){         jiemi(&ch2[i]);     }     printf("%s",ch2);     return 0; }


发表于 2022-04-20 16:54:50 回复(0)
#include<stdio.h>
#include<string.h>

int main(){
    void encrypt(); //声明加密函数
    void decrypt(); //声明解密函数
    
    char str1[1001]={'\0'};
    char str2[1001]={'\0'};
    scanf("%s",str1);
    scanf("%s",str2);
    encrypt(str1);
    decrypt(str2); 
}



void encrypt(char str[1001]){
    int len = strlen(str);
    for(int i=0;i<len;i++){
        if(str[i]>='a'&&str[i]<='y')
            printf("%c",str[i]-32+1);
        else if(str[i]>='A'&&str[i]<='Y')
            printf("%c",str[i]+32+1);
        else if(str[i]=='z')
            printf("A");
        else if(str[i]=='Z')
            printf("a");
        else if(str[i]>='0'&&str[i]<='8')
            printf("%c",str[i]+1);
        else if(str[i]=='9')
            printf("0");
    }printf("\n");
}

void decrypt(char str[1001]){
    int len = strlen(str);
    for(int i=0;i<len;i++){
        if(str[i]>='b'&&str[i]<='z')
            printf("%c",str[i]-32-1);
        else if(str[i]>='B'&&str[i]<='Z')
            printf("%c",str[i]+32-1);
        else if(str[i]=='A')
            printf("z");
        else if(str[i]=='a')
            printf("Z");
        else if(str[i]>='1'&&str[i]<='9')
            printf("%c",str[i]-1);
        else if(str[i]=='0')
            printf("9");
    }printf("\n");
}

发表于 2022-04-19 16:40:13 回复(0)
#include <stdio.h>
#include <string.h>
#define    N1    1000
#define    N2    ('a'-'A')
char* encrypt(char str1[],int len1)
{
    char *p=str1;
    int i;
    for(i=0;i<len1;i++)
    {
        if(str1[i]>='a'&&str1[i]<='z')
        {
            if(str1[i]=='z')
            {
                str1[i]='A';
            }
            else
            {
                str1[i]+=1-N2;
            }
        }
        else if(str1[i]>='A'&&str1[i]<='Z')
        {
            if(str1[i]=='Z')
            {
                str1[i]='a';
            }
            else
            {
                str1[i]+=1+N2;
            }
        }
        else if(str1[i]>='0'&&str1[i]<='9')
        {
            if(str1[i]=='9')
            {
                str1[i]='0';
            }
            else
            {
                str1[i]+=1;
            }
        }
    }
    return p;
}
char* decrypt(char str2[],int len2)
{
    char *p=str2;
    int i;
    for(i=0;i<len2;i++)
    {
        if(str2[i]>='a'&&str2[i]<='z')
        {
            if(str2[i]=='a')
            {
                str2[i]='Z';
            }
            else
            {
                str2[i]-=1+N2;
            }
        }
        else if(str2[i]>='A'&&str2[i]<='Z')
        {
            if(str2[i]=='A')
            {
                str2[i]='z';
            }
            else
            {
                str2[i]+=N2-1;
            }
        }
        else if(str2[i]>='0'&&str2[i]<='9')
        {
            if(str2[i]=='0')
            {
                str2[i]='9';
            }
            else
            {
                str2[i]-=1;
            }
        }
    }
    return p;   
}
int main()
{
    char str1[N1],str2[N1];
    scanf("%s",str1);
    scanf("%s",str2);
    int len1=strlen(str1),
        len2=strlen(str2);
    printf("%s\n",encrypt(str1,len1));
    printf("%s\n",decrypt(str2,len2));
    return 0;
}
这题很简单,为啥通过率这么低
发表于 2022-04-19 10:35:33 回复(0)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Size 1024

char *jiami(char a[Size])
{
    int len=strlen(a);
    char *b=(char*)malloc(Size*sizeof(char));
    for(int i=0;i<len;i++){
        if(a[i]=='z')b[i]='A';
        else if(a[i]=='Z')b[i]='a';
        else if(a[i]=='9')b[i]='0';
        else if(a[i]>='a'&&a[i]<'z')b[i]=a[i]-31;
        else if(a[i]>='A'&&a[i]<'Z')b[i]=a[i]+33;
        else if(a[i]>='0'&&a[i]<'9')b[i]=a[i]+1;
    }
    return b;
}

char *jiemi(char a[Size])
{
    int len=strlen(a);
    char *b=(char*)malloc(Size*sizeof(char));
    for(int i=0;i<len;i++){
        if(a[i]=='a')b[i]='Z';
        else if(a[i]=='A')b[i]='z';
        else if(a[i]=='0')b[i]='9';
        else if(a[i]>'a'&&a[i]<='z')b[i]=a[i]-33;
        else if(a[i]>'A'&&a[i]<='Z')b[i]=a[i]+31;
        else if(a[i]>'0'&&a[i]<='9')b[i]=a[i]-1;
    }
    return b;
}


int main()
{
    char a[Size]={'\0'};
    char d[Size]={0};
    while(scanf("%[^\n]",a)!=EOF){
        scanf("%*[^\n]");scanf("%*c");
        char *b=jiami(a);
        printf("%s\n",b);
        scanf("%[^\n]",d);
        scanf("%*[^\n]");scanf("%*c");
        char *c=jiemi(d);
        printf("%s\n",c);
    }
    return 0;
}

发表于 2022-01-28 22:59:18 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int main()
{
	char a[1002];
	char b[1002];
	int s1, s2,i;

	while (scanf("%s", a) != EOF)
	{
		scanf("%s", b);
		s1 = strlen(a);
		s2 = strlen(b);

		for (i = 0; i < s1; i++)
		{
			if (a[i] >= 'a' && a[i] <= 'z')
			{
				a[i] = a[i] - 32;
				if (a[i] == 'Z')
					a[i] = 'A';
				else
					a[i] = a[i] + 1;
			}
			else if (a[i] >= 'A' && a[i] <= 'Z')
			{
				a[i] = a[i] + 32;
				if (a[i] == 'z')
					a[i] = 'a';
				else
					a[i] = a[i] + 1;

			}
			else if (a[i] >= '0' && a[i] <= '9')
			{
				if (a[i] == '9')
					a[i] = '0';
				else
					a[i] = a[i] + 1;
			}
		}

		for (i = 0; i < s2; i++)
		{
			if (b[i] >= 'a' && b[i] <= 'z')
			{
				b[i] = b[i] - 32;
				if (b[i] == 'A')
					b[i] = 'Z';
				else
					b[i] = b[i] - 1;
			}
			else if (b[i] >= 'A' && b[i] <= 'Z')
			{
				b[i] = b[i] + 32;
				if (b[i] == 'a')
					b[i] = 'z';
				else
					b[i] = b[i] - 1;

			}
			else if (b[i] >= '0' && b[i] <= '9')
			{
				if (b[i] == '0')
					b[i] = '9';
				else
					b[i] = b[i] - 1;
			}
		}


		printf("%s\n", a);
		printf("%s\n", b);

	}
	return 0;
}

发表于 2022-01-23 19:42:40 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
    char str[1000] = {0};
    int i = 0;
    while(gets(str))
    {
        if(i%2 == 0)
            code(str);
        else
            decode(str);
        i++;
        printf("%s\n", str);
    }
}
int code(char str[1000])
{
    int len = strlen(str);
    int pw[1000] = {0};
    for(int i = 0;i < len;i++)
    {
        if(str[i] >='A'&&str[i]<='Z')
        {
            if(str[i] == 'Z')
                pw[i] = 'a';
            else pw[i] = str[i]+33;
        }
        else if(str[i]>='a'&&str[i]<='z')
        {
            if(str[i] == 'z')
                pw[i] == 'A';
            else pw[i] = str[i] - 31;
        }
        else if(str[i] = '9')
            pw[i] = '0';
        else pw[i] = str[i] +1;
    str[i] = pw[i];
    }
    return str;
}
int decode(char str[1000])
{
    int len = strlen(str);
    int pw[1000] = {0};
    for(int i = 0;i < len;i++)
    {
        if(str[i] >='A'&&str[i]<='Z')
        {
            if(str[i] == 'A')
                pw[i] = 'z';
            else pw[i] = str[i]+31;
        }
        else if(str[i]>='a'&&str[i]<='z')
        {
            if(str[i] == 'a')
                pw[i] == 'Z';
            else pw[i] = str[i] - 33;
        }
        else if(str[i] = '0')
            pw[i] = '9';
        else pw[i] = str[i] - 1;
    str[i] = pw[i];
    }
    return str;
}

发表于 2021-12-06 11:53:14 回复(0)