实验五

实验任务1.1

实验代码

#include <stdio.h>
#define N 4

int main()
{
	int x[N] = {1, 9, 8, 4};
	int i;
	int *p;

	// 方式1:通过数组名和下标遍历输出数组元素
	for (i = 0; i < N; ++i)
		printf("%d", x[i]);
	printf("\n");

	// 方式2:通过指针变量遍历输出数组元素 (写法1)
	for (p = x; p < x + N; ++p)
		printf("%d", *p);
	printf("\n");

	// 方式2:通过指针变量遍历输出数组元素(写法2)
	p = x;
	for (i = 0; i < N; ++i)
		printf("%d", *(p + i));
	printf("\n");
	
	// 方式2:通过指针变量遍历输出数组元素(写法3)
	p = x;
	for (i = 0; i < N; ++i)
		printf("%d", p[i]);
	printf("\n");

	return 0;
}

实验结论

实验任务1.2

实验代码

#include <stdio.h>

int main()
{
	int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
	int i, j;
	int *p; 
	int(*q)[4];

	// 使用数组名、下标访问二维数组元素
	for (i = 0; i < 2; ++i)
	{
		for (j = 0; j < 4; ++j)
			printf("%d", x[i][j]);
		printf("\n");
	}
	// 使用指针变量p间接访问二维数组元素
	for (p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i)
	{
		printf("%d", *p);
		if ((i + 1) % 4 == 0)
			printf("\n");
	}
	// 使用指针变量q间接访问二维数组元素
	for (q = x; q < x + 2; ++q)
	{
		for (j = 0; j < 4; ++j)
			printf("%d", *(*q + j));
	printf("\n");
	}

	return 0;
}

实验结论

实验任务2.1

实验代码

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

int main()
{
	char s1[] = "Learning makes me happy";
	char s2[] = "Learning makes me sleepy";
	char tmp[N];

	printf("sizeof(s1) vs. strlen(s1): \n");
	printf("sizeof(s1) = %d\n", sizeof(s1));
	printf("strlen(s1) = %d\n", strlen(s1));

	printf("\nbefore swap: \n");
	printf("s1: %s\n", s1);
	printf("s2: %s\n", s2);

	printf("\nswapping...\n");
	strcpy(tmp, s1);
	strcpy(s1, s2);
	strcpy(s2, tmp);

	printf("\nafter swap: \n");
	printf("s1: %s\n", s1);
	printf("s2: %s\n", s2);
	
	return 0;
}

实验结论

回答问题

1.s1的大小是24个字节,sizeof(s1)计算的是s1的大小,也就是能容纳s1的最小字节数。strlen(s1)统计的是s1的长度。
2.不能,在定义字符数组时,需要指定数组的大小,以便编译器可以为数组分配足够的内存空间。因此,不能在定义字符数组时省略数组大小。并且字符数组不支持直接赋值操作,所以可以借助函数strcpy()来给s1赋值。
3.s1和s2的内容已经完成交换

实验任务2.2

实验代码

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

int main()
{
	char *s1 = "Learning makes me happy";
	char *s2 = "Learning makes me sleepy";
	char *tmp;

	printf("sizeof(s1) vs. strlen(s1): \n");
	printf("sizeof(s1) = %d\n", sizeof(s1));
	printf("strlen(s1) = %d\n", strlen(s1));

	printf("\nbefore swap: \n");
	printf("s1: %s\n", s1);
	printf("s2: %s\n", s2);

	printf("\nswapping...\n");
	tmp = s1;
	s1 = s2;
	s2 = tmp;

	printf("\nafter swap: \n");
	printf("s1: %s\n", s1);
	printf("s2: %s\n", s2);
	
	return 0;
}

实验结论

回答问题

问题1:指针变量s1中存放的是字符串常量"Learning makes me happy"的地址。sizeof(s1)计算的是指针变量s1的大小,地址值长度为8个字节。strlen(s1)统计的是字符串常量"Learning makes me happy"的长度。

问题2:可以替换。line7的写法是将字符串常量的地址赋给指针变量s1,而task2_1.c的写法则是将指向字符数组的指针变量s1初始化为对应的字符数组的首地址。二者的语义是一样的。

问题3:line20-line22交换的是指针变量s1和s2的值,即它们所指向的地址值。字符串常量本身在内存中是不可变的,所以它们所对应的存储单元中并没有发生交换。

实验任务3

实验代码

#include <stdio.h>

void str_cpy(char *target, const char *source);
void str_cat(char *str1, char *str2);

int main()
{
	char s1[80], s2[20] = "1984";

	str_cpy(s1, s2);
	puts(s1);

	str_cat(s1, " Animal Farm");
	puts(s1);

	return 0;
}
void str_cpy(char *target, const char *source)
{
	while (*target++ = *source++);
}
void str_cat(char *str1, char *str2)
{
	while (*str1)
		str1++;
		
	while (*str1++ = *str2++);
}

实验结论

实验任务4

实验代码

#include <stdio.h>
#define N 80
int func(char *);

int main()
{
	char str[80];

	while (gets(str) != NULL)
	{
		if (func(str))
			printf("yes\n");
		else
			printf("no\n");
	}

	return 0;
}

int func(char *str)
{
	char *begin, *end;

	begin = end = str;

	while (*end)
		end++;

	end--;

	while (begin < end)
	{
		if (*begin != *end)
			return 0;
		else
		{
			begin++;
			end--;
		}
	}

	return 1;
}

实验结论

实验任务5

实验代码

#include <stdio.h>
#define N 80

void func(char *);

int main()
{
	char s[N];

	while (scanf("%s", s) != EOF)
	{
		func(s);
		puts(s);
	}

	return 0;
}

void func(char *str)
{
	int i;
	char *p1, *p2, *p;

	p1 = str;
	while (*p1 == '*')
		p1++;
	p2 = str;
	while (*p2)
		p2++;
	p2--;
	
	while (*p2 == '*')
		p2--;
	
	p = str;
	i = 0;
	while (p < p1)
	{
		str[i] = *p;
		p++;
		i++;
	}
	
	while (p <= p2)
	{
		if (*p != '*')
		{
			str[i] = *p;
			i++;
		}
		p++;
	}

	while (*p != '\0')
	{
		str[i] = *p;
		p++;
		i++;
	}
	
	str[i] = '\0';
}

实验结论

实验任务6.1

实验代码

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

int main()
{
	char *course[4] = {"C Program",
		"C++ Object Oriented Program",
		"Operating System",
		"Data Structure and Algorithms"};
	int i;

	sort(course, 4);

	for (i = 0; i < 4; i++)
		printf("%s\n", course[i]);
	
	return 0;
}
void sort(char *name[], int n)
{
	int i, j;
	char *tmp;

	for (i = 0; i < n - 1; ++i)
		for (j = 0; j < n - 1 - i; ++j)
			if (strcmp(name[j], name[j + 1]) > 0)
			{
			tmp = name[j];
			name[j] = name[j + 1];
			name[j + 1] = tmp;
			}
}

实验结论

实验任务6.2

实验代码

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

int main()
{
	char *course[4] = {"C Program",
		"C++ Object Oriented Program",
		"Operating System",
		"Data Structure and Algorithms"};
	int i;

	sort(course, 4);
	for (i = 0; i < 4; i++)
		printf("%s\n", course[i]);

	return 0;
}

void sort(char *name[], int n)
{
	int i, j, k;
	char *tmp;

	for (i = 0; i < n - 1; i++)
{
		k = i;
		for (j = i + 1; j < n; j++)
			if (strcmp(name[j], name[k]) < 0)
				k = j;
		if (k != i)
		{
			tmp = name[i];
			name[i] = name[k];
			name[k] = tmp;
		}
	}
}

实验结论

回答问题

如图示,交换的是指针变量的值。通过交换指针变量的值,可以交换指向字符串的指针,从而改变输出的顺序,而实际存储在内存中的字符串的位置不会改变。

实验任务7

实验代码

#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){
	if (strlen(str) != 18)
        return 0;

    int i;
    for (i = 0; i < 18; ++i)
        if (!(str[i] >= '0' && str[i] <= '9') && str[i] != 'X')
            return 0;

    return 1;
}

实验结论

实验任务8

实验代码

#include <stdio.h> 1
#define N 80
void encoder(char *s);
void decoder(char *s);

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 *s)
{
    while (*s != '\0') {
        if ((*s >= 'a' && *s < 'z') || (*s >= 'A' && *s < 'Z')) {
            *s = *s + 1;
        } else if (*s == 'z') {
            *s = 'a';
        } else if (*s == 'Z') {
            *s = 'A';
        }
        s++;
    }
}

void decoder(char *s)
{
    while (*s != '\0') {
        if ((*s > 'a' && *s <= 'z') || (*s > 'A' && *s <= 'Z')) {
            *s = *s - 1;
        } else if (*s == 'a') {
            *s = 'z';
        } else if (*s == 'A') {
            *s = 'Z';
        }
        s++;
    }
}

实验结论