|
发表于 2021-11-27 11:15:43
|
显示全部楼层
你写的代码呢?你就打算在这里要一个正确答案然后交差了事?
这题目真的不难,你写不出来,说明你基础学的不好,可以返回前面去温故知新
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- void encode(char *buff, const char *str, unsigned char key) {
- if(buff != str) strcpy(buff, str);
- for(size_t i = 0; buff[i]; ++i) {
- if(!islower(buff[i])) continue;
- buff[i] = 'a' + (buff[i] - 'a' + key) % 26;
- }
- }
- void decode(char *buff, const char *str, unsigned char key) {
- if(buff != str) strcpy(buff, str);
- for(size_t i = 0; buff[i]; ++i) {
- if(!islower(buff[i])) continue;
- buff[i] = 'z' + (buff[i] - 'z' - key) % -26;
- }
- }
- void decode2(char *buff, const char *str, unsigned char key) {
- encode(buff, str, 26 - key);
- }
- int main(void) {
- char buff[1024];
- const char *str = "abcdABCD;1234[]9876;ZXCVzxcv-";
- encode(buff, str, 3);
- puts(str);
- puts(buff);
- decode(buff, buff, 3);
- puts(buff);
- puts("");
- encode(buff, str, 3);
- puts(str);
- puts(buff);
- decode2(buff, buff, 3);
- puts(buff);
- return 0;
- }
复制代码 |
|