C++将一个字符串按字符大小排序,数字字符排在字母后面,要求定义函数实现,不使用库函数,求源代码。
2条回答 默认 最新
技术专家团-小桥流水 2021-11-15 14:42关注代码如下:
#include <iostream> #include <string> using namespace std; void fun(char buf[]) { int i,j; char ch; int n = strlen(buf); for (i=0;i<n-1;i++) { for (j=0;j<n-1-i;j++) { if(buf[j] > buf[j+1]) { ch = buf[j]; buf[j]=buf[j+1]; buf[j+1]= ch; } } } //cout << buf << endl; //将数字字符移动到末尾 //找到数字字符的起始位置和终止位置#4342abv int start=0,end = 0,k=0; char* st = new char[n+1]; for(i=0;i<n;i++) { if(buf[i]>='0' && buf[i]<='9') { st[k++] = buf[i]; if(start=0&&end==0) start = i; else end = i; } } st[k] = 0; //cout << st<<endl; //数组迁移 for (i=start;i<n-(end-start+1);i++) { buf[i]= buf[i+end-start+1]; } //cout << buf << endl; for (i=0;i<end-start+1;i++) { buf[n-i] = st[end-start-i]; } delete[] st; st=0; } int main() { char buf[100]={0}; cin.getline(buf,100); fun(buf); cout << buf; return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报