我想对这个数组进行排序,但是如果我没有在数组中放入任何带有特殊字符的字符串,那么这段代码就可以工作。如果我有类似的东西
!\"#$%&'()*+,-./0123456789:;<=>?@
它不会工作。它在 Visual Studio 中崩溃。
这是代码:
#include <iostream>
#include <cstring>
using namespace std;
int main (){
char data[10][40] = {
"",
"Welcome",
" !\"#$%&'()*+,-./0123456789:;<=>?@",
"aBCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
"abcdefghijklmnopqrstuvwxyZ{||||||||||}",
"CD_ROM",
"ROM",
"SCS",
"3.5 Floppi",
""
};
cout<<"Printing the array as is"<<endl<<endl;
for (int i=0; i<10; i++){
cout<<data[i]<<endl;
}
cout<<endl<<"Ordering the data in Alphabetical order"<<endl<<endl;
// bubble sort
for (int i=0 ; i<10-1 ; ++i) {
char Tcopy[17];
for (int j=i+1 ; j<10 ; ++j) {
if (strcmp(data[i], data[j]) > 0) {
strcpy(Tcopy, data[i]);
strcpy(data[i], data[j]);
strcpy(data[j], Tcopy);
}
}
}
cout<<"Printing the array Sorted"<<endl<<endl;
for (int i=0; i<10; i++){
cout<<data[i]<<endl;
}
// Pause
cout<<endl<<endl<<endl<<"Please Close Console Window"<<endl;
cin.ignore('\n', 1024);
return(0);
}