void sortArray(char* array[], int count){
int compare;
int index;
for(index = 0; index < count; index++){ //runs through array, keeping track of index
for(compare = index; compare < count; compare++){
if (strcmp(array[compare] , array[index]) <= 0){
swap(*(array+compare), *(array+index));
}
}
}
}
void swap(char *strA, char *strB){
char *temp = (char *)malloc((strlen(strA)+1) * sizeof(char));
assert(temp!=NULL);
strcpy(temp, strA);
free(strA);
strA=(char *)malloc((strlen(strB)+1) * sizeof(char));
assert(strA!=NULL);
strA=strcpy(strA, strB);
free(strB);
strB= (char *)malloc((strlen(temp)+1) * sizeof(char));
assert(strB!=NULL);
strcpy(strB, temp);
free(temp);
}
出力を与えます:
Array1[0]: Adam Pendleton
Array1[1]: Jeison Ortega
Array1[2]: Theodore Staake
Array1[3]: Patrick Mahoney
Array1[4]: Andrew Silberstein
Array1[5]: Alan Boulais
Array1[6]: Daniel Comeau
Array1[7]: Sean Mikhael
Array1[8]: Sarah Shank
Array1[9]: Daniel Verge
Array1[10]: Dhimitris Natsis
Array1[11]: Kathleen Lent
Array1[12]: Osei Collins
Array1[13]: Jason Hintlian
Array1[14]: Michael Gibson
Array1[15]: Alex Grossi
Array1[16]: Michael Svedberg
Array1[17]: Randall White
Array1[18]: Alvin Cordor
Array1[19]: Rodrigo Roldan
Array1[20]: Matthew McArthur
Array1[21]: Jesse Anaya
Sorted Array1[0]: Adam Pendleton
Sorted Array1[1]: Patrick Mahoney
Sorted Array1[2]: Theodore Staake
Sorted Array1[3]: Sarah Shank
Sorted Array1[4]: Dhimitris Natsis
Sorted Array1[5]: Alan Boulais
Sorted Array1[6]: Alex Grossi
Sorted Array1[7]: Alvin Cordor
Sorted Array1[8]: Sean Mikhael
Sorted Array1[9]: Osei Collins
Sorted Array1[10]: Michael Svedberg
Sorted Array1[11]: Daniel Comeau
Sorted Array1[12]: Daniel Verge
Sorted Array1[13]: Jason Hintlian
Sorted Array1[14]: Jesse Anaya
Sorted Array1[15]: Michael Gibson
Sorted Array1[16]: Matthew McArthur
Sorted Array1[17]: Randall White
Sorted Array1[18]: Rodrigo Roldan
Sorted Array1[19]: Kathleen Lent <-----not sure why this is dupe
Sorted Array1[20]: <-----not sure why this is here
Sorted Array1[21]: Kathleen Lent
これらの空白がどこから来ているのか、なぜ重複した名前があるのか わかりません。最初と最後の文字列を交換しただけのreverseArrayという別の関数を作成しましたが、問題はswap関数に起因するようです.文字列をコピーしているときに混乱が生じていると思います.
これは起こっていることのほんの一部であるため、ファイルから 1 行ずつテキストを読み込んで、char* 配列 [] に名前の文字列を入力しました。並べ替えました。