实验5

task1.1

1.找到一串数的最大最小值

2.指向数组首个元素

task1.2

1.找到最大值,返回最大值地址

2.可以

task2.1

1.80 计算s1的大小 统计字符串长度

2.不能,s1数组无法直接修改

task2.2

1.存放字符串的地址,计算s1的占用空间,统计字符串长度

2.能,上一个是试图直接编写数组,这个是传递地址

3.交换的是指针指向的地址,内存并未交换

 task4

 1.将字符串中的所有某个字符替换成某个字符

2.可以

#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){
    while (*str) {
        if (*str == x)
            break;
        str++;
    }
   while(*str){
       *str = 0;
       str++;
   }
}
// 函数str_trunc定义
// 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址
// 待补足...
// xxx

image

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

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

int main()
{
    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(char *str) {
    for (int i=0;i<18;i++) {
        if ((*str < '0' || *str>'9') && *str != 'X')
            return 0;
        else if (*str == 'X' && i!= 17)
            return 0;
        else {
            str++;
        }
    }
    return 1;
    // 补足函数实现
    // ...
}

image

 

#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_s("%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) {
    while (*str != 0) {
        if ((*str >= 'a' && *str <= 'z' - n) || (*str >= 'A' && *str <= 'Z' - n))
            *str += n % 26;
        else if ((*str >= 'z'-n && *str <= 'z') || (*str >= 'Z'-n && *str <= 'Z'))
            *str += n % 26 - 26;
            str++;
    }
    // 补足函数实现
    // ×××
}

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

image

#include <stdio.h>
#include <string.h>
int p(int n, char* q[]);
int main(int argc, char* argv[]) {
    int i;
    p(argc, argv);
    for (i = 1; i < argc; ++i)
        printf("hello, %s\n", argv[i]);

    return 0;
}
int p(int n, char* q[]) {
    char* t;
    for (int i = 1; i < n ; i++) {
        for (int j = 1; j < n- i; j++) {
            if (strcmp(q[j], q[j + 1]) > 0)
            {
                t = q[j];
                q[j] = q[j + 1];
                q[j + 1] = t;
            }
        }
    }
}

image

 

posted @ 2025-12-11 20:22  even_233  阅读(4)  评论(0)    收藏  举报