700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 金山笔试题-字符串排序 : 写一个函数 实现对给定的字符串(字符串里面包括:英文字

金山笔试题-字符串排序 : 写一个函数 实现对给定的字符串(字符串里面包括:英文字

时间:2023-05-15 16:35:04

相关推荐

金山笔试题-字符串排序 : 写一个函数 实现对给定的字符串(字符串里面包括:英文字

写一个函数,实现对给定的字符串(字符串里面包括:英文字母,数字,符号)的处理。经过处理后的字符串其内容按字母,数字,符号的顺序存放。函数声明如下:

voidParseString(char*pstr);

要求:

a.不能改函数声明;

b.不改变字母数字等在字符串中原有的出现顺序;

c.直接使用pstr所值指缓冲区,不允许另开缓冲区。

本来不会做,问了同学才知道用冒泡法,比较规则按类型比较

// maopao.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <algorithm>#include <cstring>using namespace std;// 大于 1 等于0 小于 -1int compare(char a,char b){if(isalpha(a)){if(isalpha(b)){return 0;}else{return 1;}}else if(isdigit(a)){if(isalpha(b)){return -1;}else if(isdigit(b)){return 0;}else {return 1;}}else{if(isalpha(b) || isdigit(b)){return -1;}else {return 0;}}}void ParseString(char* pstr){bool changed = false;int n = strlen(pstr);do {changed =false;for(int i=1;i<n;++i){if( compare(pstr[i-1],pstr[i])<0 ){changed =true;swap(pstr[i-1],pstr[i]);}}--n;} while (changed);}int _tmain(int argc, _TCHAR* argv[]){char buf[] = "A,2.d?3!e4r87we79";ParseString(buf);printf("%s\n",buf);system("pause");return 0;}

金山笔试题-字符串排序 : 写一个函数 实现对给定的字符串(字符串里面包括:英文字母 数字 符号)的处理...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。