Files
Learn-Algorithms/9 Algorithms Job Interview/1.4 字符串-排序.md
T
2021-09-20 13:28:54 +08:00

55 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 字符串-排序
## 小写字母排在大写字母的前面
有一个由大小写组成的字符串,现在需要对他进行修改,将其中的所有小写字母排在大写字母的前面(大写或小写字母之间不要求保持原来次序),如有可能尽量选择时间和空间效率高的算法 c语言函数原型:
```
void proc(char *str)
```
分析:
比如:HaJKPnobAACPc,要小写字母前面且不要求保存顺序,可以是:anobcHJKPAACP
1. 小写字母 a~z 的 ASCII码值是 97~122,A~Z的 ASCII码值是 65~90;0~9 的ASCII码是 48~57
2. 两边向中间扫描,左边大写右边小写就交换;如果都小写,头指针向前知道找到大写;如果都是大写,尾指针向后找小写;
示例代码
```
char *proc(char *str){
char *start = str;
char *end = str;
if (str == NULL) return NULL;
while(*end != '\0') end++;
end--;
while(start < end){
if (*start >= 'A' && *start <= 'Z'){//大写
if (*end >= 'a' && *end <= 'z'){
char tmp = *start;
*start = *end;
*end = tmp;
start++;
}
end--;
}else{//小写
if (*end >= 'A' && *end <= 'Z'){
end--;
}
start++;
}
}
return str;
}
```