|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 壳970527 于 2022-11-10 00:08 编辑
#include <stdio.h>
#include <string.h>
void printStrs(char **q, int n);
void swapStrs(char **q, int i, int j);
int main(void)
{
char *p[100];
char osaka[] = "Osaka", kyoto[] = "Kyoto", shiga[] = "Shiga",
nara[] = "Nara", hyogo[] = "Hyogo", wakayama[] = "Wakayama";
int i, j;
p[0] = osaka; p[1] = kyoto; p[2] = shiga; p[3] = nara; p[4] = hyogo; p[5] = wakayama;
printStrs(p, 6);
while (1) {
printf("Input a pair of integers: ");
scanf("%d %d", &i, &j);
if(i < 0 || i > 5 || j < 0 || j > 5) break;
swapStrs(p, i, j);
printStrs(p, 6);
}
return 0;
}
void printStrs(char **q, int n)
{
int i;
printf("------ begin: print_string ------\n");
printf("0: %p %s\n", q[0],q[0]);
printf("1: %p %s\n", q[1],q[1]);
printf("2: %p %s\n", q[2],q[2]);
printf("3: %p %s\n", q[3],q[3]);
printf("4: %p %s\n", q[4],q[4]);
printf("5: %p %s\n", q[5],q[5]);
printf("------ end: print_string ------\n");
}
void swapStrs(char **q, int i, int j)
{
char temp;
temp = *q[i];
*q[i] = *q[j];
*q[j] = temp;
}
上面是写的代码,我交换之后发现只有头字母交换可是后面的字母全都没有交换这是为什么呀,哪里出现了问题,希望大佬能够回答下,谢谢!!
#include <stdio.h>
#include <string.h>
void printStrs(char **q, int n);
void swapStrs(char **q, int i, int j);
int main(void) {
char *p[100];
char osaka[] = "Osaka", kyoto[] = "Kyoto", shiga[] = "Shiga",
nara[] = "Nara", hyogo[] = "Hyogo", wakayama[] = "Wakayama";
int i, j;
p[0] = osaka;
p[1] = kyoto;
p[2] = shiga;
p[3] = nara;
p[4] = hyogo;
p[5] = wakayama;
printStrs(p, 6);
while(1) {
printf("Input a pair of integers: ");
scanf("%d %d", &i, &j);
if(i < 0 || i > 5 || j < 0 || j > 5)
break;
swapStrs(p, i, j);
printStrs(p, 6);
}
return 0;
}
void printStrs(char **q, int n) {
printf("------ begin: print_string ------\n");
printf("0: %p %s\n", q[0], q[0]);
printf("1: %p %s\n", q[1], q[1]);
printf("2: %p %s\n", q[2], q[2]);
printf("3: %p %s\n", q[3], q[3]);
printf("4: %p %s\n", q[4], q[4]);
printf("5: %p %s\n", q[5], q[5]);
printf("------ end: print_string ------\n");
}
void swapStrs(char **q, int i, int j) {
char *temp;
temp = q[i];
q[i] = q[j];
q[j] = temp;
}
|
|