#include <stdio.h> #include <string.h> #define ROWS 5 #define COLUMNS 20 /* @Filename: ex603.c @Author: Ju Chengdong @Version: 1.0 @Date: 2021-03-18 @Description: Sort Words @Features: 基于字符数组指针(行指针)实现 */ /* * 函数名称:assign * 函数功能:通过键盘输入为多个一维字符数组(字串)赋值 * 形式参数:arr为一维字符数组指针(行指针);rows为int型,单词字串个数 * 返 回 值:无 */ void assign(char (*arr)[COLUMNS], int rows){ // 请编码实现assign函数
时间: 2023-06-19 07:09:01 浏览: 82
void assign(char (*arr)[COLUMNS], int rows){
printf("Please enter %d words:\n", rows);
for(int i=0; i<rows; i++){
scanf("%s", arr[i]);
}
}
// 注意:这里是二维字符数组,所以arr的类型为char (*)[COLUMNS],不是char**。
相关问题
/* * 函数名称:assign * 函数功能:通过键盘输入为多个一维字符数组(字串)赋值 * 形式参数:arr为一维字符数组指针(行指针);rows为int型,单词字串个数 * 返 回 值:无 */ void assign(char (*arr)[COLUMNS], int rows){ // 请编码实现assign函数 } /* * 函数名称:show * 函数功能:输出显示所有单词(字串) * 形式参数:arr为一维字符数组指针(行指针);rows为int型,单词字串个数 * 返 回 值:无 */ void show(char (*arr)[COLUMNS], int rows){ // 请编码实现show函数 } /* * 函数名称:sort * 函数功能:对多个一维字符数组(字串)进行排序 * 形式参数:arr为一维字符数组指针(行指针);rows为int型,单词字串个数 * 返 回 值:无 */ void sort(char(*arr)[COLUMNS], int rows){ // 请编码实现sort函数 } int main(void){ //ch为二维字符数组,包含ROWS个单词,每个单词最多COLUMNS个字母 char ch[ROWS][COLUMNS]; memset(ch,0,sizeof(ch)); //p是一个指向字符数组的指针,称为行指针,请参见教材Page189 //必须给p指定长度,否则编译不报错,运行也不报错,但会不正常 //p与ch等价,因此既可引用p,也可引用ch char (*p)[COLUMNS] = ch; //从键盘输入ROWS个单词,为ch赋值 assign(p, ROWS); //对ch中的ROWS个单词进行排序 sort(p, ROWS); //输出显示单词排序结果 show(p, ROWS); return 0; }
这是一个关于多个一维字符数组的排序和显示的程序。主函数定义了一个二维字符数组 ch,包含 ROWS 个单词,每个单词最多 COLUMNS 个字母。然后定义一个字符数组指针 p,指向 ch。程序中有三个函数:
1. assign:通过键盘输入为多个一维字符数组(字串)赋值。
2. show:输出显示所有单词(字串)。
3. sort:对多个一维字符数组(字串)进行排序。
需要补全这三个函数的实现。具体实现如下:
```c
#include <stdio.h>
#include <string.h>
#define ROWS 10
#define COLUMNS 20
void assign(char (*arr)[COLUMNS], int rows){
printf("请输入 %d 个单词:\n", rows);
for(int i=0; i<rows; i++){
scanf("%s", arr[i]);
}
}
void show(char (*arr)[COLUMNS], int rows){
printf("单词排序结果为:\n");
for(int i=0; i<rows; i++){
printf("%s\n", arr[i]);
}
}
void sort(char(*arr)[COLUMNS], int rows){
char temp[COLUMNS];
for(int i=0; i<rows-1; i++){
for(int j=0; j<rows-1-i; j++){
if(strcmp(arr[j], arr[j+1]) > 0){
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j+1]);
strcpy(arr[j+1], temp);
}
}
}
}
int main(void){
//ch为二维字符数组,包含ROWS个单词,每个单词最多COLUMNS个字母
char ch[ROWS][COLUMNS];
memset(ch,0,sizeof(ch));
//p是一个指向字符数组的指针,称为行指针,请参见教材Page189
//必须给p指定长度,否则编译不报错,运行也不报错,但会不正常
//p与ch等价,因此既可引用p,也可引用ch
char (*p)[COLUMNS] = ch;
//从键盘输入ROWS个单词,为ch赋值
assign(p, ROWS);
//对ch中的ROWS个单词进行排序
sort(p, ROWS);
//输出显示单词排序结果
show(p, ROWS);
return 0;
}
```
在 sort 函数中,使用了 strcmp 函数来比较字符串大小,使用了 strcpy 函数来交换两个字符串。在 main 函数中,先将指针 p 指向 ch,然后调用 assign 函数为 ch 赋值,调用 sort 函数对 ch 进行排序,最后调用 show 函数输出排序结果。
写一个aes加密算法的代码
以下是一个简单的 AES 加密算法的代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define Nb 4
#define Nk 4
#define Nr 10
typedef uint8_t state_t[4][4];
static const uint8_t sbox[256] = {
// S-box
};
static const uint8_t rsbox[256] = {
// Inverse S-box
};
static const uint8_t Rcon[11] = {
// Round constants
};
static void sub_bytes(state_t *state)
{
// SubBytes step
}
static void shift_rows(state_t *state)
{
// ShiftRows step
}
static void mix_columns(state_t *state)
{
// MixColumns step
}
static void add_round_key(uint8_t round, state_t *state, const uint8_t *key)
{
// AddRoundKey step
}
static void key_expansion(const uint8_t *key, uint8_t *w)
{
// KeyExpansion routine
}
void aes_encrypt(const uint8_t *in, uint8_t *out, const uint8_t *key)
{
state_t state;
uint8_t w[4 * Nb * (Nr + 1)];
key_expansion(key, w);
memcpy(state, in, 4 * Nb);
add_round_key(0, &state, key);
for (uint8_t round = 1; round < Nr; ++round) {
sub_bytes(&state);
shift_rows(&state);
mix_columns(&state);
add_round_key(round, &state, w + round * 4 * Nb);
}
sub_bytes(&state);
shift_rows(&state);
add_round_key(Nr, &state, w + Nr * 4 * Nb);
memcpy(out, state, 4 * Nb);
}
int main(void)
{
uint8_t in[16] = { /* Input data */ };
uint8_t out[16];
uint8_t key[16] = { /* Key */ };
aes_encrypt(in, out, key);
for (int i = 0; i < 16; ++i) {
printf("%02x ", out[i]);
}
printf("\n");
return 0;
}
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)