1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【C基础练习题】Week9:凯撒密码 | 按空格切分字符串 | 单词折半拆分

【C基础练习题】Week9:凯撒密码 | 按空格切分字符串 | 单词折半拆分

时间:2022-08-12 23:41:35

相关推荐

【C基础练习题】Week9:凯撒密码 | 按空格切分字符串 | 单词折半拆分

目录

第一题:凯撒密码

第二题:按空格切分字符串

第三题:单词折半拆分

第一题:凯撒密码

【百度百科】在密码学中,恺撒密码(英语:Caesar cipher),或称恺撒加密恺撒变换变换加密,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。

💬 加密数据的方法有很多种,其中最基础的是凯撒密码算法(Caesar Cipher Algorithm),

请写一个程序完成此算法。加密方法满足下列关系式:

比如:key2P"ABCDE"时,则C"CDEFG"

(Plain text 为加密前的内容,Cipher text 为加密后的内容)

具体要求:

① 限制要加密的内容长度为30以下,以储存。

② 只加密26个字母(大小写),其余文字都不用加密。

③ 定义一个 encryption 函数,加密部分在此函数内实现。

运行结果演示:(测试语句:Hello world! I'm LeiJun, Are you ok?)

🔑 参考答案:

#include <stdio.h>#include <string.h>#include <stdlib.h>char* encryption (char* P, int key) {int len = strlen(P);char* Ciphertext = (char*)malloc((len + 1) * sizeof(char));for (int i = 0; i < len; i++) {if ('A' <= P[i] && P[i] <= 'Z') {Ciphertext[i] = (P[i] - 'A' + key) % 26 + 'A';} else if ('a' <= P[i] && P[i] <= 'z') {Ciphertext[i] = (P[i] - 'a' + key) % 26 + 'a';} else {Ciphertext[i] = P[i];} }Ciphertext[len] = '\0';return Ciphertext;}int main(void) { int key, len, i = 0;char ch, P[31], * C;printf("Input the plain text: ");while (1) {scanf("%c", &ch);if(ch == '\n') {break;}P[i++] = ch;}P[i] = '\0';while (1) {printf("Input key: ");scanf("%d", &key);if (key >= 0 && key < 26) {break;} else {printf("0 <= key <= 26\n");continue;}}C = encryption(P, key);printf("Encrypt(%s) = %s\n", P, C);printf("Decrypt(%s) = %s\n", C, encryption(C, 26 - key)); //利用encryption函数解密if (C != NULL) {free(C);}return 0;}

第二题:按空格切分字符串

💬 接受一串字符串,将这段字符串的所有的单词分行输出。

限制要求:

① 接收的字符串字符限制为40,允许用户输入 40 个以上的字符,但最多只能接收到40 个字符。

③无论是字母、特殊字符还是数字还是单词,都按照空格来为分割。

运行结果演示:

🔑 参考答案:

#include <stdio.h>void printByWord(char* str) {char* p = str;printf("\t");while (*p != '\0') {if (*p == ' ') {printf("\n\t");}else if (*p != '\n') {printf("%c", *p);}p++;}printf("\n");}int main(void) {char str[40];printf("Input string : ");scanf("%40[^\n]", str);printf("- Result - \n");printByWord(str);}

🚩 运行结果:

第三题:单词折半拆分

编写一个程序,将一个单词拆分为两个单词。

实现用如下函数,将一个单词一分为二:

void splitString(char *str, char *word1, char *word2);

(该函数将 str 分成两个子串 word1 和 word2)

如果 str 的长度为偶数 (2n),则拆分为两个长度相等 (n) 的字符串。

如果 str 的长度为奇数(2n+1),则让 word2 分得更多些(word1 : word2 = n : n+1)。

运行结果演示:

🔑 参考答案:

#include <stdio.h>#include <string.h>#include <stdlib.h>void splitString(char* str, char* word1, char* word2) {int i = 0;int n = (int)strlen(str);for (i = 0; i < n / 2; i++) {word1[i] = str[i];}word1[n / 2] = '\0';for (i = n / 2; i < n; i++) {word2[i - n / 2] = str[i];}word2[n - n / 2] = '\0';}int main(void) {char* word1, *word2;char input[30];int n = 0;printf("input str : ");scanf("%30[^\n]", input);n = (int)strlen(input) / 2;word1 = (char*)malloc(sizeof(char) * (n + 1));word2 = (char*)malloc(sizeof(char) * (n + 2));splitString(input, word1, word2);printf("[%s] -> [%s] [%s]\n", input, word1, word2);free(word1);word1 = NULL;free(word2);word2 = NULL;return 0;}

🚩 运行结果:

参考资料

Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

百度百科[EB/OL]. []. /.

📌 笔者:王亦优

📃 更新: .12.14

❌ 勘误: 无

📜 声明: 由于作者水平有限,本文有错误和不准确之处在所难免,本人也很想知道这些错误,恳望读者批评指正!

本篇完。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。