将一个数字字符串,比如“4783912”排序后再拷贝给另一个字符串.咋弄啊,小弟,菜鸟,希望大家帮帮忙!!!
将一个数字字符串,比如“4783912”排序后再拷贝给另一个字符串char *(char *strDest, char *strSrc)
{
}
char* copy(char *strDest, char *strSrc) { int i; for (i = 0; i < 7; i ++) { strDest[i] = strSrc[i]; } return NULL; } 或者可以用Strcpy(***, ***)函数来调用 排序可以用冒泡 char sort(char *name, int n) { int i, j; for (i = 0; i < n; i ++) { for (j = i+1; j < n; j ++) { char temp; if ( name[i] > name[j]) { temp = name[i]; name[i] = name[j]; name[j] = temp; } } } return name[i]; }
#include<iostream.h> #include<string.h> class SortCopy { public: SortCopy(char *name1); ~SortCopy(); char sort(); // 排序字串中的内容 void copy(char *strDest, char *strSrc); // 把原字串赋给目标字串 void show(); private: char *name; // 字符串 }; SortCopy::SortCopy(char *name1) { name = new char[strlen(name1) + 1]; strcpy(name, name1); } SortCopy::~SortCopy() { delete []name; } char SortCopy::sort() // 冒泡排序 { int i, j; for (i = 0; i < 8; i ++) { for (j = i+1; j < 8; j ++) { char temp; if(name[i] > name[j]) { temp = name[i]; name[i] = name[j]; name[j] = temp; } } } return name[i]; } void SortCopy::copy(char *strDest, char *strSrc) // 调用strcpy函数 { strDest = name; strDest = new char[strlen(strSrc) + 1]; strcpy(strDest, strSrc); } void SortCopy::show() { int i = 0; for(i = 0; i < 8; i++) cout << name[i] << " "; } void main() { char *ia = "4783912"; SortCopy ob(ia); cout << "原来的字符串;" << endl; ob.show(); cout << endl; cout << "排序:" << endl; ob.sort(); ob.show(); cout << endl; char *ib; cout << "赋给ib:" << endl; ob.copy(ib, ia); ob.show(); cout << endl; } 参考一下吧,这是用c++做的,当然也能用c做,主要函数差不多的