实验五

实验1

 

 

功能是分别找到输入数中的最小值和最大值

两者都指向x[0]

功能是找到最大数,返回的是最大数的地址

可以,因为改变之后返回的也是最大值的地址

 

实验2

 

 

s1大小是24个字节,sizeof以字节为单位返回一个变量,表达式或类型的长度,

strlen是计算字符串长度的,不包含结尾符号‘\0’

不能,因为定义数组s1时,没规定长度

内容交换了

sizeof(s1)可能是4或8,看指针变量在不同计算机中所占字节数

strlen(s1)同上

可以,一个是定义数组内容,一个是将指针指向一个字符串的地址

实验3

一个代替数组名称的指针

一个指针数组

实验四

将字符串中的某个旧字符该成新字符

不可以,那样的话新的字符串末尾没有结束符号

 

实验5

交换的是指针变量的值

实验六

#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 a=1;
    	if(strlen(str)!=18)
    	return 0;
    		//a=0;
    	int i;
    	for(i=0;i<18;i++)
    	{
    		if(str[i]<48||str[i]>57)
    		{
    			if(str[i]!=88)
    			{
    				return 0;
    			//	a=0;
    			//break;
				}
			}
		}
		
    
	
	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;
}

/*函数定义
功能:对s指向的字符串进行编码处理
编码规则:
对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换
其它非字母字符,保持不变
*/
void encoder(char *str) {
    while(*str)
    {
        if((*str<122&&*str>=97)||(*str<90&&*str>=65))
        {
            *str+=1;
        }
        else if(*str=122)
            *str=97;
        else if(*str=90)
            *str=65;
        else
            *str=*str;
        str++;
    }
    
}

/*函数定义
功能:对s指向的字符串进行解码处理
解码规则:
对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换
其它非字母字符,保持不变
*/
void decoder(char *str) {
        while(*str)
    {
        if((*str<=122&&*str>97)||(*str<=90&&*str>65))
        {
            *str-=1;
        }
        else if(*str=97)
            *str=122;
        else if(*str=6)
            *str=90;
        else
            *str=*str;
        str++;
    }
    
}

 

posted @ 2023-12-03 21:56  陈星我儿  阅读(29)  评论(0)    收藏  举报