鱼C论坛

 找回密码
 立即注册
查看: 1452|回复: 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
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 20

  4. void encryption(char* str, size_t n) {
  5.     for (char* p = str; p < str + n; ++p) {
  6.         if (((*p) >= 'a' && (*p) <= 'w') || ((*p) >= 'A' && (*p) <= 'W')) {
  7.             (*p) += 3;
  8.         }
  9.         else if (((*p) >= 'x' && (*p) <= 'z') || ((*p) >= 'X' && (*p) <= 'Z')) {
  10.             (*p) -= 23;
  11.         }
  12.     }
  13. }

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

使用道具 举报

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

  4. void encryption(char* str, size_t n) {
  5.     for (char* p = str; p < str + n; ++p) {
  6.         if (((*p) >= 'a' && (*p) <= 'w') || ((*p) >= 'A' && (*p) <= 'W')) {
  7.             (*p) += 3;
  8.         }
  9.         else if (((*p) >= 'x' && (*p) <= 'z') || ((*p) >= 'X' && (*p) <= 'Z')) {
  10.             (*p) -= 23;
  11.         }
  12.     }
  13. }

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

使用道具 举报

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

  4. void encryption(char* str, size_t n) {
  5.     for (char* p = str; p < str + n; ++p) {
  6.         if (((*p) >= 'a' && (*p) <= 'w') || ((*p) >= 'A' && (*p) <= 'W')) {
  7.             (*p) += 3;
  8.         }
  9.         else if (((*p) >= 'x' && (*p) <= 'z') || ((*p) >= 'X' && (*p) <= 'Z')) {
  10.             (*p) -= 23;
  11.         }
  12.     }
  13. }

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

使用道具 举报

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

借花献佛,装个B
  1. void encryption(char* str, size_t n) {
  2.     for (char* p = str; *p; ++p) {
  3.                
  4.         if (((*p) >= 'a' && (*p) <= 'z') || ((*p) >= 'A' && (*p) <= 'Z')) {
  5.                 *p+=n;
  6.                 if (((*p) < 'a' || (*p) > 'z') && ((*p) < 'A' || (*p) > 'Z'))
  7.                         if(n<0)*p+=26;
  8.                         else
  9.                                 *p-=26;
  10.                        
  11.         }
  12.       
  13.     }
  14. }
  15. /*
  16. input pos num(-5<pos<5): 3

  17. input string: Hello the world!

  18. output string:Khoor wkh zruog!
  19. */
  20. int main(void)
  21. {
  22.         int m=0;
  23.         printf("input pos num(-5<pos<5):");
  24.         scanf("%d",&m);
  25.         fflush(stdin);
  26.         printf("\ninput string:");
  27.         //char ch=getchar();
  28.         ungetc(getchar(),stdin);
  29.         //n=stdin->_cnt;
  30.         char* input=(char*)malloc(stdin->_cnt);
  31.         gets(input);
  32.         encryption((char*)input,m);
  33.         printf("output string:%s", input);
  34.         free(input);
  35.         return 0;
  36. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 17:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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