鱼C论坛

 找回密码
 立即注册
查看: 1610|回复: 3

[已解决]求助用数组和字符串解决凯撒加密

[复制链接]
发表于 2022-5-23 18:01:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
基于字符串实现整串文字的“凯撒加密”

1)定义字符数组 char input[N] 保存输入的文字

2)使用gets获取输入的整串文字,并保存在 input 数组

3)基于字符指针遍历数组,循环执行凯撒加密

4)输出字符仍然保存在 input 数组

5)整行打印转意后的字符

例如:

input pos num(-5<pos<5): 3

input string: Hello the world!

output string:Khoor wkh zruog!

  要求使用字符指针遍历
  谢谢各位大佬啦!
最佳答案
2022-5-23 18:26:02
#include <stdio.h>
#include <string.h>
#define N 20

void encryption(char* str, size_t n) {
    for (char* p = str; p < str + n; ++p) {
        if (((*p) >= 'a' && (*p) <= 'w') || ((*p) >= 'A' && (*p) <= 'W')) {
            (*p) += 3;
        }
        else if (((*p) >= 'x' && (*p) <= 'z') || ((*p) >= 'X' && (*p) <= 'Z')) {
            (*p) -= 23;
        }
    }
}

int main(void) {
    char input[N];
    gets(input);
    encryption((char*)input, strlen(input));
    printf("%s", input);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 18:26:02 | 显示全部楼层    本楼为最佳答案   
#include <stdio.h>
#include <string.h>
#define N 20

void encryption(char* str, size_t n) {
    for (char* p = str; p < str + n; ++p) {
        if (((*p) >= 'a' && (*p) <= 'w') || ((*p) >= 'A' && (*p) <= 'W')) {
            (*p) += 3;
        }
        else if (((*p) >= 'x' && (*p) <= 'z') || ((*p) >= 'X' && (*p) <= 'Z')) {
            (*p) -= 23;
        }
    }
}

int main(void) {
    char input[N];
    gets(input);
    encryption((char*)input, strlen(input));
    printf("%s", input);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-5-23 19:15:49 | 显示全部楼层
#include <stdio.h>
#include <string.h>
#define N 20

void encryption(char* str, size_t n) {
    for (char* p = str; p < str + n; ++p) {
        if (((*p) >= 'a' && (*p) <= 'w') || ((*p) >= 'A' && (*p) <= 'W')) {
            (*p) += 3;
        }
        else if (((*p) >= 'x' && (*p) <= 'z') || ((*p) >= 'X' && (*p) <= 'Z')) {
            (*p) -= 23;
        }
    }
}

int main(void) {
    char input[N];
    gets(input);
    encryption((char*)input, strlen(input));
    printf("%s", input);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-5-23 20:45:27 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-23 21:41 编辑

借花献佛,装个B
void encryption(char* str, size_t n) {
    for (char* p = str; *p; ++p) {
                
        if (((*p) >= 'a' && (*p) <= 'z') || ((*p) >= 'A' && (*p) <= 'Z')) { 
                *p+=n;
                if (((*p) < 'a' || (*p) > 'z') && ((*p) < 'A' || (*p) > 'Z'))
                        if(n<0)*p+=26;
                        else
                                *p-=26;
                        
        }
       
    }
}
/*
input pos num(-5<pos<5): 3

input string: Hello the world!

output string:Khoor wkh zruog!
*/
int main(void)
{
        int m=0;
        printf("input pos num(-5<pos<5):");
        scanf("%d",&m);
        fflush(stdin);
        printf("\ninput string:");
        //char ch=getchar();
        ungetc(getchar(),stdin);
        //n=stdin->_cnt;
        char* input=(char*)malloc(stdin->_cnt);
        gets(input);
        encryption((char*)input,m);
        printf("output string:%s", input);
        free(input);
        return 0;
} 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 00:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表