实验5

一、1-1、① 使最大值、最小值赋值到对应指针变量

                ②全部指向 X首元素地址

       1-2、①最大值地址

                ②可以

二、2-1、①24     数组s1占用字节数    数组s1长度

                ②不能  数组s1没有初值和大小,不能正常创建

                ③是

       2-2、①字符串 “。。。happy” 的地址      指针变量s1的大小     字符串 “。。。happy” 的长度

                ②可以   前者为给数组s1赋值,后者为把 “。。。happy” 的地址赋值给指针s1

                ③指针s1、s2中存储的地址     没有

三、3-1、①输出2将 X首元素地址 赋值给ptr1,再按地址顺序不断输出

                   输出3将 X地址(即首元素地址)赋值给ptr2,*ptr2+j 还是地址,需要再次*

四、4-1、①按地址顺序,把所有和 oldchar 样的字符换成 newchar

                ②可以

       4-2、

#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) {
    while(*str)
    {
        if(*str == x)
        {
            *(str) = '\0';
            break;
        }
        str++;
    }
}

 

 

 

五、5、①指针变量的值

六、6-1、

 

#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;
}

// 函数定义
// 功能: 检查指针str指向的身份证号码串形式上是否合法。
// 形式合法,返回1,否则,返回0
int check_id(char *str)
{
    int l=0;
    while(*str)
    {
        if( *str<'0' || *str>'9')
        {
            if( *str != 'X')
                return 0;
        }
        str++;
        l++;
    }
    
    if(l != 18)
        return 0;
    else
        return 1;
}

 

 

 七

#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>='a' && *str<='y') || (*str>='A' && *str<='Y'))
            *str += 1;
        else if( *str == 'z' || *str == 'Z')
            *str -= 25;
        str++;
    }
}

void decoder(char *str)
{
    while(*str)
    {
        if( (*str>='b' && *str<='z') || (*str>='B' && *str<='Z'))
            *str -= 1;
        else if( *str == 'a' || *str == 'A')
            *str += 25;
        str++;
    }
}

 

 
#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;

    for(i = 1; i < argc; ++i)
        printf("hello, %s\n", argv[i]);

    return 0;
}

 

 

 

posted @ 2023-11-27 19:08  黄骑  阅读(18)  评论(0)    收藏  举报