前几天去某个公司面试也遇到类似的问题,现用c++写了个简单的 C/C++ code #include <stdio.h> #include <string.h> //判读一个字符是否为字母,不区分大小写 bool isAlphabet(char ch) { return ((ch >= 65 && ch <= 90)||(ch >= 97 && ch <= 122)); } //从两头到中间找,然后替换 char* reverseStringByOrder(char * str) { int length=strlen(str);//字符串长度 int left=0,right=length-1; while(left<right) { //跳过非字母 while(!isAlphabet(str[left])) { left++; } //跳过非字母 while(!isAlphabet(str[right])) { right--; } ///////////交换两个字母 char temp=str[left]; str[left]=str[right]; str[right]=temp; left++; right--; } return str; } int main(int argc, char* argv[]) { char test []="1w4rt,5t7?9u"; printf("the result:%s/n",reverseStringByOrder(test)); return 0; }