例如:AAbbCC应该是AACCbb
页面上一切正常,但由于某种原因,当函数交换值时,str
的值发生变化(在运行结束时,程序打印 AAC 的值)
这是我的代码:
void capital_to_Low(char *str)
{
int i = 0, j = (strlen(str)) -1;
while (i < j)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
swap(&str[i], &str[j--]);
}
else i++;
}
puts(str);
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
最佳答案
将 void swap(int *a, int *b) 更改为 void swap(char *a, char *b) ,因为您指的是字符地址而不是整数
关于c - 在 c 中对一串大写字母和小写字母进行排序(通过交换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48668416/