实验5 ———

1.1

找到最大值和最小值

数组 x 的第一个元素 x[0] 的值

1.2

最大值所在元素的地址

不,地址不能交换

 

2.1

1__23     sizeof(s1) 计算的是数组 s1 在内存中所占用的字节数 strlen(s1) 统计的是字符串 s1 中的字符数

2__不能    未分配地址

3__能

2.2

1__s1 不再是一个数组,而是一个指向字符的指针 sizeof(s1) 将返回指针的大小 strlen(s1) 统计的是指针 s1 所指向的字符串的长度

2__能

2__地址中的内容

 

 

 

test4

#include <stdio.h>
#define N 80
void str_trunc(char* str, char x);
int main() {
    char str[N];
    char ch;
    printf("输入字符串: ");
    gets(str);
    printf("输入一个字符: ");
    ch = getchar();
    printf("截断处理...\n");
    str_trunc(str, ch);
    printf("截断处理后的字符串: %s\n", str);
}
void str_trunc(char* str, char x) {
    int  qwe = 0;
    while (*str!='\0') {
        if (*str == x) {
            
            qwe = 1;
        }
        if (qwe == 1) {
            *str = '\0';
        }
        
        str++;
        
    }
    
}

 

 

 

 

test 6

#include <stdio.h>
#include <string.h>
#define N 5
int check_id(char* str); // 函数声明
int main()
{
    char* pid[N] = { "31010120000721656X",
    "330106199609203301",
    "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;
}
int check_id(char* str) {
    int length = 0;
    while (*str != '\0') {
        length++;
        str++;
    }
    if (length != 18) {
        return 0;
    }
    str--;
    
    while (length>0) {
        if (!((*str>='0' && *str <= '9') || *str == 'X')) {
            return 0;

        }
        str--;
        length--;

    }
    return 1;


}

 

 

 

test 7

 

#include <stdio.h>
#define N 80
void encoder(char* str); 
void decoder(char* str); 
int main() {
    char words[N];
    printf("输入英文文本: ");
    gets(words);
    printf("编码后的英文文本: ");
    encoder(words); 
    printf("%s\n", words);
    printf("对编码后的英文文本解码: ");
    decoder(words);
    printf("%s\n", words);
    return 0;
}

void encoder(char* str) {
    while (*str) {
        if (*str <= 'z' && *str >= 'a') {
            if (*str == 'z') {
                *str = 'a';
            }
            else {
                *str += 1;
            }

        }
        else if(*str <= 'Z' && *str >= 'A') {
            if (*str == 'Z') {
                *str = 'A';
            }
            else {
                *str += 1;
            }
        }
        str++;
    }
}

void decoder(char* str) {
    while (*str) {
        if (*str <= 'z' && *str >= 'a') {
            if (*str == 'a') {
                *str = 'z';
            }
            else {
                *str -= 1;
            }

        }
        else if (*str <= 'Z' && *str >= 'A') {
            if (*str == 'A') {
                *str = 'Z';
            }
            else {
                *str -= 1;
            }
        }
        str++;
    }











}

 

posted @ 2023-12-03 17:45  李宇晨呵呵  阅读(30)  评论(0)    收藏  举报