实验五:指针与字符串实验
实验题目(1)【见实验教材实验六的题目2】:编程exp6_2.c,现有整型变量x,y,调用交换函数以期实现两个值的交换。下表中4种不同的定义及调用分别进行测试并填写表格。
表1 拟实现交换的四种方法
原型声明 | void swap1( int , int ); | void swap2( int *, int ); | void swap3( int *, int *); | void swap4( int *, int *); |
调用 语句 | swap1( x , y ); | swap2( &x , y ); | swap3( &x , &y ); | swap4( &x , &y ); |
函数 定义 | void swap1(int a, int b ) { int temp= a; a = b; b = temp; } | void swap2(int *a, int b ) { int temp= *a; *a = b; b = temp; } | void swap3(int *a, int *b ) { int temp = *a; *a = *b; *b = temp; } | void swap4(int *a, int *b ) { int *temp = a; a=b; b=temp; } |
实验解答:
① 主函数代码如下:
int main()
{
int x=1,y=2;
swap1(x,y); /*此处依次换成另外3种调用进行观察,其余代码不变*/
printf("x=%d,y=%d\n",x,y);
return 0;
}
通过修改以上主函数中调用函数的语句,按表1更新对应的函数调用,分别运行程序,填写下表:
函数原型 | 输出结果 | 是否交换 | 原因分析 |
void swap1( int a, int b ); | x=1,y=2 | 否 | 形参的值交换了,但是实参的值没有交换。形参和实参占用的是两组不同的空间,具有不同的作用域。 |
void swap2( int *a, int b ); | x=2,y=2 | 否 | 传地址可以实现数值的交换,传值无法让实参的值交换。 |
void swap3( int *a, int *b ); | x=2,y=1 | 是 | 主调函数中的变量的作用域扩展到被调函数中。 |
void swap4(int *a, int *b ); | x=1,y=2 | 否 | 被调函数修改的是指针本身,而不是指针指向的值。 |
② 利用F10和F11功能键进行单步跟踪,4次运行观察各变量的变化情况,填写下表:
跟踪点(黄色箭头所指行) | 实参x的值 | 实参y的值 | 跟踪点(黄色箭头所指行) | 形参a(或*a)的值 | 形参b(或*b)的值 |
swap1调用行 | 1 | 2 | swap1函数左大括号处 | a的值: 1 | b的值:2 |
swap1后的printf行 | 1 | 2 | swap1函数右大括号处 | a的值:2 | b的值: 1 |
swap2调用行 | 1 | 2 | swap2函数左大括号处 | *a的值:1 | b的值:2 |
swap2后的printf行 | 2 | 2 | swap2函数右大括号处 | *a的值:2 | b的值: 1 |
swap3调用行 | 1 | 2 | swap3函数左大 号处 | *a的值: 1 | *b的值: 2 |
swap3后的printf行 | 2 | 1 | swap3函数右大括号处 | *a的值:2 | *b的值: 1 |
swap4调用行 | 1 | 2 | swap4函数左大括号处 | *a的值:1 | *b的值:2 |
swap4后的printf行 | 1 | 2 | swap4函数右大括号处 | *a的值:2 | *b的值: 1 |
实验题目(2)【见实验教材实验六的题目4】: 编写程序exp6_4.c,实现数组的逆置。如:int a[5]={1,3,4,5,6,7};逆置后int a[5]={7,6,4,5,3,1}
实验解答:
- 写出完整的源程序代码并做适当注释:
# include <stdio.h>
void reverse (int array[],int n) //将数组前后对称的两个元素交换
{
int i,j,temp;
for (i=0,j=n-1;i<j;i++,j--)
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
int main()
{
int str[20];
int n,i;
do
{
scanf("%d",&n);
}while(1>n||n>20); //输入数组元素个数(1<=n<=20)
for (i=0;i<n;i++)
{
scanf("%d",&str[i]); //输入数组元素
}
reverse (str,n); //实现逆序置换
for(i=0;i<n;i++)
printf("%2d",str[i]); //打印交换后的数组
return 0;
}
②运行一次程序,记录程序运行的结果:
6
1 2 3 4 5 6
6 5 4 3 2 1
实验题目(3)【见实验教材实验七的题目3】:编写程序exp7_3.c,从键盘读入一个字符串,去掉其中所有的空格得到一个新串后再输出(只能定义一个字符数组,不可以定义两个字符数组,可以根据编程需要定义字符指针以及其他变量)。
实验解答:
①写出完整的源程序代码并做适当注释:
#include <stdio.h>
char * str_del(char* str)
{
int i=0,j=0;
while(str[i])
{
if(str[i]!=' ') //判断是否是空格
str[j++]=str[i++]; //不是空格则将字符赋值给str[j]
else i++; //是空格则将下标加1
}
str[j]='\0'; //在str[j]的结尾加上\0结束,否则会编译错误
return str;
}
int main()
{
char cstr[20];
printf("Input a string:");
gets(cstr); //输入字符串
str_del(cstr);
puts(cstr); //输出字符串
return 0;
}
②按表中所给测试用例输入观察对应输出结果:
测试用例要求 | 输入的原始串 | 输出结果串 |
串中空格每处只有一个 | A bcd 12 45 t | Abcd1245t |
至少有一处有连续多个空格字符 | A bc 12 t | Abc12t |
字符串最前面是4个空格 | A b 12 t | Ab12t |
你自己设计的测试用例 | a f d s e e eeeee3 | afdseeeeeee3 |
实验题目(4)【见实验教材实验七的题目2】:: 编写程序exp7_2.c,帮助小红从小明通过网络发送的两串字符串中,提取用户名和密码。
提取规则:将第一串字符串中所有的大小写字母按原序连起来,再按原序取奇数位就是用户名;将第二串中所有数字按原序连起来,再按原序取偶数位就是密码。例:小明发给小红的第一个字符串为“ag13re5D6s789h0R”,则源串大小字母按序连起来为“agreDshR”,所以用户名为“arDh”;第二个字符串为“UGa4fF6hHt97J89iK5i4L2P3”, 则源串数字按序连起来为“4697895423”,所以密码为“67943”。
实验解答:
- 写出完整的源程序代码并做适当注释:
#include<stdio.h>
#include<string.h>
char *letter(char *cstr)
{
int i=0,j=0;
while(cstr[i])
{
if(('A'<=*(cstr+i)&&*(cstr+i)<='Z')||('a'<=*(cstr+i)&&*(cstr+i)<='z'))
cstr[j++]=cstr[i++];
else i++;
}
cstr[j]='\0';
return cstr;
}
char *number(char *str)
{
int i=0,j=0;
while(str[i])
{
if('0'<=*(str+i)&&*(str+i)<='9')
str[j++]=str[i++];
else i++;
}
str[j]='\0';
return str;
}
char *choice1(char*name,char *name1)
{
int i=0,j=0;
int m=strlen(name);
while(name[i])
{
i=2*j;
name1[j++]=name[i++];
}
name1[j]='\0';
return name1;
}
char *choice2(char*password,char * password1)
{
int i=0,j=0;
int a=strlen(password);
while(i<a)
{
i=2*j+1;
password1[j++]=password[i++];
}
password1[j]='\0';
return password1;
}
int main()
{
char put1[200],put2[200],put3[200],put4[200];
printf("Input a string:");
gets(put1);
letter(put1);
choice1(put1,put3);
printf("The use name is:");
puts(put3);
printf("Input a string:");
gets(put2);
number(put2);
choice2(put2,put4);
printf("The password is:");
puts(put4);
return 0;
}
②记录程序运行结果,观察用户名和密码字符串的具体输出结果:
Input a string:ag13re5D6s789h0R
The use name is:arDh
Input a string:UGa4fF6hHt97J89iK5i4L2P3
The password is:67943