实验5

task1_1

问题一:找出数组x中的最大值与最小值。
问题二:指向数组第一个元素。

task1_2

问题一:找到数组中最大值所在的地址。
问题二:可以

task2_1

问题一:80,s1这个数组所占的字节数,s1从第一个元素到'\0'之间的元素数
问题二:不能,s1是指针,不能直接赋值。
问题三:是。

task2_2

问题一:存放的是该字符串首字母所在的地址。指针变量 s1 本身占用的字节数。该字符串从第一个元素到'\0'之间的元素数。
问题二:不能,改法是直接对指针s1进行了赋值,是错误的。而原版是使s1指向该字符串首字母所在的地址。
问题三:交换的是指针 s1 和 s2 中存储的地址

task4

问题一:将str指向的字符串中的old_char改为new_char
问题二:可以

task5

源代码:

#include <stdio.h>
#define N 80

char* str_trunc(char* str, char x);

int main() {
    char str[N];
    char ch;

    while (printf("输入字符串: "), gets(str) != NULL) {
        printf("输入一个字符: ");
        ch = getchar();

        printf("截断处理...\n");
        str_trunc(str, ch);         // 函数调用

        printf("截断处理后的字符串: %s\n\n", str);
        getchar();
    }

    return 0;
}
char* str_trunc(char* str, char x) {
    char* k = str;
    while (*str) {
        if (*str == x) {
            *str = '\0';
            break;
        }
        str++;
    }
    return k;
}

运行截图:
屏幕截图 2025-12-05 170250

task6

源代码:

#include <stdio.h>
#include <string.h>
#define N 5

int check_id(const char* str); // 函数声明

int main()
{
    const char *pid[N] = {"31010120000721656X",
                    "3301061996X0203301",
                    "53010220051126571",
                    "510104199211197977",
                    "53010220051126133Y" };
    int i;

    for (i = 0; i < N; ++i)
        if (check_id(pid[i])) // 函数调用
            printf("%s\tTrue\n", pid[i]);
        else
            printf("%s\tFalse\n", pid[i]);

    return 0;
}

// 函数定义
// 功能: 检查指针str指向的身份证号码串形式上是否合法
// 形式合法,返回1,否则,返回0
int check_id(const char* str) {
    if (strlen(str) != 18) return 0;
    for (int i = 0; i < 17; ++i) {
        if (!(str[i] >= '0' && str[i] <= '9')) { 
            return 0;
        }
    }
    char last_char = str[17];
    if (!((last_char >= '0' && last_char <= '9') || last_char == 'X' || last_char == 'x')) {
        return 0;
    }

    return 1;
}

运行截图:

屏幕截图 2025-12-08 194025

task7

源代码:

#include <stdio.h>
#define N 80
void encoder(char* str, int n); // 函数声明
void decoder(char* str, int n); // 函数声明

int main() {
    char words[N];
    int n;

    printf("输入英文文本: ");
    gets(words);

    printf("输入n: ");
    scanf("%d", &n);

    printf("编码后的英文文本: ");
    encoder(words, n);      // 函数调用
    printf("%s\n", words);

    printf("对编码后的英文文本解码: ");
    decoder(words, n); // 函数调用
    printf("%s\n", words);

    return 0;
}

/*函数定义
功能:对str指向的字符串进行编码处理
编码规则:
对于a~z或A~Z之间的字母字符,用其后第n个字符替换; 其它非字母字符,保持不变
*/
void encoder(char* str, int n) {
    for (int i = 0; str[i] != '\0'; ++i) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = 'a' + (str[i] - 'a' + n) % 26;
        }
        else if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = 'A' + (str[i] - 'A' + n) % 26;
        }
    }
}

/*函数定义
功能:对str指向的字符串进行解码处理
解码规则:
对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变
*/
void decoder(char* str, int n) {
    for (int i = 0; str[i] != '\0'; ++i) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = 'a' + (str[i] - 'a' - n + 26) % 26;
        }
        else if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = 'A' + (str[i] - 'A' - n + 26) % 26;
        }
    }
}

运行截图:image
image
image

task8

源代码:

#include <stdio.h>
#include<string.h>
void sort(int n, char* s[]);

int main(int argc, char* argv[]) {
    int i;
    sort(argc - 1, &argv[1]);
    for (i = 1; i < argc; ++i)
        printf("hello, %s\n", argv[i]);

    return 0;
}
void sort(int n, char* s[]) {
    int i, j;
    char* tmp;
    for (i = 0; i < n - 1; ++i) {
        for (j = 0; j < n - i - 1; ++j) {
            if (strcmp(s[j], s[j + 1]) > 0) {
                tmp = s[j];
                s[j] = s[j + 1];
                s [j+ 1] = tmp;
            }
        }
    }
}

运行截图:

屏幕截图 2025-12-05 175011
image

posted @ 2025-12-08 20:16  沈月汐  阅读(0)  评论(0)    收藏  举报