#include <stdio.h>
#include <string.h>
main()
{
int j, i=0, temp;
printf(“请输入你想排序的数字:\n”);
char str[100];
scanf(“%c”, &str[i]);
while (str[i]!=”\n”)
{
i++;
scanf(“%c”, &str[i]);
}
printf(“%d”,strlen(str));
for (j = 0;j < strlen(str);j++)
for (i = 0; i < strlen(str) – j; i++)
if (str[i] < str[i+1])
{
temp = str[i];
str[i] = str[i+1];
str[i+1] = temp;
}
printf(“冒泡排序后的值为:\n”);
for (i = 0; i <strlen(str); i++)
printf(“%c “, str[i]);
}
可以成功排序,但是输出的结果后边会有一串乱码,应该怎么判断?
#include <string.h>
main()
{
int j, i=0, temp;
printf(“请输入你想排序的数字:\n”);
char str[100];
scanf(“%c”, &str[i]);
while (str[i]!=”\n”)
{
i++;
scanf(“%c”, &str[i]);
}
printf(“%d”,strlen(str));
for (j = 0;j < strlen(str);j++)
for (i = 0; i < strlen(str) – j; i++)
if (str[i] < str[i+1])
{
temp = str[i];
str[i] = str[i+1];
str[i+1] = temp;
}
printf(“冒泡排序后的值为:\n”);
for (i = 0; i <strlen(str); i++)
printf(“%c “, str[i]);
}
可以成功排序,但是输出的结果后边会有一串乱码,应该怎么判断?
解决方案
40
在char str[100]后面加上memset(str, 0, 100)这句代码就好了