#include <stdio.h>
#include "string.h"
int main()
{
char s[20];
scanf("%s",s);
int i = 0;
int j = 0;
for (i = 0; i < 19; i++) {
int m = i;
for (j = i + 1; j < 20; j++) {
if (s[m] > s[j]) {
m = j;
}
}
int t = s[i];
s[i] = s[m];
s[m] = t;
}
for (i = 0; i < 20; i++)
printf("%c", s[i]);
return 0;
}
运行结果
可能是你输出的时候忘记用strlen了?
#include <stdio.h>
#include "string.h"
int main()
{
char s[20];
scanf("%s", s);
int i = 0;
int j = 0;
for (i = 0; i < strlen(s)-1; i++) {
int m = i;
for (j = i + 1; j < strlen(s); j++) {
if (s[m] > s[j]) {
m = j;
}
}
int t = s[i];
s[i] = s[m];
s[m] = t;
}
for (i = 0; i < strlen(s); i++)
printf("%c", s[i]);
return 0;
}
输入字符串的时候,字符串末尾有一个多的\0
题目要求是不多于20个字符串,最好是定义一个[21]的数组,以存放末尾的\0
你的问题应该是第一个for循环里面的strlen忘记-1了
访问的时候越界了
如果对你有帮助,还请点个采纳,万分感谢!