最近,在刷题的时候发现了一把好用的锤子,以后就不用手写字符串倒序函数了。
//字符串倒序算法
//利用快排函数qsort,比较字符的内存位置
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *p1, const void *p2);
int main(int argc, int* argv[]){
char strA[32] = "I Love You";
qsort(strA, strlen(strA), sizeof(char), cmp);
printf("%s\n", strA);
return 0;
}
int cmp(const void *p1, const void *p2){
return p1 > p2 ? 0 : 1;
}
测试结果如下: