需求
对字符串前半部分排序,如果长度是技术,则中间的字符不变
代码实现
int sortHalfStr(char * str){
if(NULL == str){
printf("strSortSwap param error\n");
return PARAM_ERR;
}
int len = strlen(str);
int i = 0, j = 0, sz = len / 2;
char * p = str;
char * t = str + sz - 1;
char tmp;
for(i = 0; i < sz; i++){
for(j = i + 1; j < sz; j++){
if(p[i] < p[j]){
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
return SUCCESS;
}
void testsortHalfStr(void){
char str[100] = "ADZDDJKJFIEJHGI";
printf("\n************ testsortHalfStr ************ \n");
sortHalfStr(str);
printf("after sort half stirng is : %s\n", str);
return;
}
代码编译
gcc main.c str.c -g -o a.exe
调试输出
************ testsortHalfStr ************
after sort half stirng is : ZKJDDDAJFIEJHGI