用c语言解决问题3.下面程序是用指针法将一个字符串a复制到字符串b中,修改程序实现问题(1)(2)。void main(){char a[]="I am a boy.",b[20], *p1,*p2;int i;p1=a;p2=b;for( ; *p1!=’\0’;p1++,p2++)*p2=*p1;*p2=’\0’;printf("string a is:%s\n",a);printf("string b is:");for(i=0;b[i]!=’\0’;i++)printf("%c",b[i]);}(1)将第一个for循环改为while循环;(2)分别用指针变量p1和p2输出字符串a 和b。
时间: 2023-11-28 21:50:17 浏览: 80
使用指针、数组处理在C语言中处理“字符串”
(1) 将第一个for循环改为while循环:
```c
void main(){
char a[]="I am a boy.";
char b[20];
char *p1 = a;
char *p2 = b;
int i = 0;
while (*p1 != '\0') {
*p2 = *p1;
p1++;
p2++;
}
*p2 = '\0';
printf("string a is: %s\n", a);
printf("string b is: %s\n", b);
}
```
(2) 分别用指针变量p1和p2输出字符串a和b:
```c
void main(){
char a[]="I am a boy.";
char b[20];
char *p1 = a;
char *p2 = b;
int i = 0;
while (*p1 != '\0') {
*p2 = *p1;
p1++;
p2++;
}
*p2 = '\0';
printf("string a is: ");
while (*p1 != '\0') {
printf("%c", *p1);
p1++;
}
printf("\nstring b is: ");
while (*p2 != '\0') {
printf("%c", *p2);
p2++;
}
}
```
阅读全文