首页 > 试题广场 >

字符串加解密

[编程题]字符串加解密
  • 热度指数:256732 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

对输入的字符串进行加解密,并输出。

加密方法为:

当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;

当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;

其他字符不做变化。

解密方法为加密的逆过程。
数据范围:输入的两个字符串长度满足 ,保证输入的字符串都是只由大小写字母或者数字组成

输入描述:

第一行输入一串要加密的密码
第二行输入一串加过密的密码



输出描述:

第一行输出加密后的字符
第二行输出解密后的字符

示例1

输入

abcdefg
BCDEFGH

输出

BCDEFGH
abcdefg
#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)
#include <stdio.h>
#include <string.h>

int main(){
    char s[100][1000],t[100][1000];
    int cnt=0,i=0,j=0;
    while (gets(s[i++])){
        cnt++;
    }
    
    for(j=0;j<cnt;j++){
        if(j%2==0){
            int n=strlen(s[j]);
            for(i=0;i<n;i++){
                if(s[j][i]>='A'&&s[j][i]<='Y') t[j][i]=s[j][i]+'a'-'A'+1;
                else if(s[j][i]=='Z')  t[j][i]='a';
                else if(s[j][i]>='a'&&s[j][i]<='y') t[j][i]=s[j][i]+'A'-'a'+1;
                else if(s[j][i]=='z')  t[j][i]='A';
                else if(s[j][i]>='0'&&s[j][i]<='8') t[j][i]=s[j][i]+1;
                else if(s[j][i]=='9')  t[j][i]='0';
                     else t[j][i]=s[j][i];
            }
        t[j][n]='\0';
        printf("%s\n",t[j]);
        }
        else{
            int m=strlen(s[j]);
            for(i=0;i<m;i++){
                if(s[j][i]>='B'&&s[j][i]<='Z') t[j][i]=s[j][i]+'a'-'A'-1;
                else if(s[j][i]=='A')  t[j][i]='z';
                else if(s[j][i]>='b'&&s[j][i]<='z') t[j][i]=s[j][i]+'A'-'a'-1;
                else if(s[j][i]=='a')  t[j][i]='Z';
                else if(s[j][i]>='1'&&s[j][i]<='9') t[j][i]=s[j][i]-1;
                else if(s[j][i]=='0')  t[j][i]='9';
                    else t[j][i]=s[j][i];
                }
            t[j][m]='\0';
            printf("%s\n",t[j]);
        }
    }
    return 0;
    }
    

发表于 2021-10-18 13:50:40 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
    char str1[1000];
    char str2[1000];
    while(scanf("%s",&str1)!=EOF)
    {
        scanf("%s",&str2);
        int len1=strlen(str1);
        int len2=strlen(str2);
        int i;
        for(i=0;i<len1;i++)
        {
            if((str1[i]>='A')&&(str1[i]<='Y'))
                printf("%c",str1[i]+33);
            else if(str1[i]=='Z')
                printf("%c",'a');
            else if((str1[i]>='a')&&(str1[i]<='y'))
                printf("%c",str1[i]-31);
            else if(str1[i]=='z')
                printf("%c",'A');
            else if((str1[i]>='0')&&(str1[i]<='8'))
                printf("%c",str1[i]+1);
            else if(str1[i]=='9')
                printf("%c",'0');
            else 
                printf("%c",str1[i]);
        }
        printf("\n");
        for(i=0;i<len2;i++)
        {
            if((str2[i]>='B')&&(str2[i]<='Z'))
                printf("%c",str2[i]+31);
            else if(str2[i]=='A')
                printf("%c",'z');
            else if((str2[i]>='b')&&(str2[i]<='z'))
                printf("%c",str2[i]-33);
            else if(str2[i]=='a')
                printf("%c",'Z');
            else if((str2[i]>='1')&&(str2[i]<='9'))
                printf("%c",str2[i]-1);
            else if(str2[i]=='0')
                printf("%c",'9');
            else 
                printf("%c",str2[i]);
        }
        printf("\n");
        }
}

发表于 2021-09-06 12:29:49 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char str[500] = {0};
    char str_out[500] = {0};
    char str1[500] = {0};
    
    while(gets(str))
    {
        gets(str1);
        memset(str_out, '\0',sizeof(str_out));
        for(int i = 0; str[i] != '\0'; i++)
        {
            if(str[i] >= 'a' && str[i] <= 'z')
            {
                str_out[i] = (str[i]%'a'+1)%26+'A';
            }
            else if(str[i] >= 'A' && str[i] <= 'Z')
            {
                str_out[i] = (str[i]%'A'+1)%26+'a';
            }
            else if(str[i] >= '0' && str[i] <= '9')
            {
                str_out[i] = (str[i]%'0'+1)%10+'0';
            }
        }
        printf("%s\n", str_out);
        memset(str_out, '\0',sizeof(str_out));
        for(int i = 0; str1[i] != '\0'; i++)
        {
            if(str1[i] >= 'a' && str1[i] <= 'z')
            {
                str_out[i] = (str1[i]%'a'-1+26)%26 +'A';
            }
            else if(str1[i] >= 'A' && str1[i] <= 'Z')
            {
                str_out[i] = (str1[i]%'A'-1+26)%26 +'a';
            }
            else if(str1[i] >= '0' && str1[i] <= '9')
            {
                str_out[i] = (str1[i]%'0'-1+10)%10 +'0';
            }
        }
        printf("%s\n", str_out);
    }
}

发表于 2021-07-17 15:20:00 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str1[100],str2[100];
    int i,j;
    while(gets(str1))
    {
        gets(str2);
        int len1,len2;
        len1=strlen(str1);
        len2=strlen(str2);
//         char output1[100],ooutput2[100];
        for(i=0;i<len1;i++)
        {
            if(str1[i]<='z'&&str1[i]>='a')
            {
                if(str1[i]=='z')
                {
                    str1[i]='A';
                }
                else
                {
                    str1[i]=str1[i]+1-'a'+'A';
                }
            }
            else if(str1[i]<='Z'&&str1[i]>='A')
            {
                if(str1[i]=='Z')
                {
                    str1[i]='a';
                }
                else
                {
                    str1[i]=str1[i]+1-'A'+'a';
                }
            }
            else if(str1[i]<='9'&&str1[i]>='0')
            {
                str1[i]=(str1[i]-'0'+1)%10+'0';
            }
        }
        for(i=0;i<len2;i++)
        {
            if(str2[i]<='z'&&str2[i]>='a')
            {
                if(str2[i]=='a')
                {
                    str2[i]='Z';
                }
                else
                {
                    str2[i]=str2[i]-1-'a'+'A';
                }
            }
            else if(str2[i]<='Z'&&str2[i]>='A')
            {
                if(str2[i]=='A')
                {
                    str2[i]='z';
                }
                else
                {
                    str2[i]=str2[i]-1-'A'+'a';
                }
            }
            else if(str2[i]<='9'&&str2[i]>='0')
            {
                if(str2[i]=='0')
                {
                    str2[i]='9';
                }
                else
                {
                    str2[i]=str2[i]-1;
                }
            }
        }
        printf("%s\n",str1);
        printf("%s\n",str2);
    }
    return 0;
}

发表于 2021-07-15 14:11:00 回复(0)

问题信息

难度:
18条回答 49077浏览

热门推荐

通过挑战的用户

查看代码