#import <Foundation/Foundation.h>
void sortArray(char (*string)[20], int n){
int flag = 1;
while (flag) {
flag = 0;
for (int i = 0; i < n - 1; i++) {
if (strcmp(*(string + i),*(string + i + 1)) > 0) {
char tmp[20];
strcpy(tmp, *(string + i));
strcpy(*(string + i), *(string + i + 1));
strcpy(*(string + i + 1), tmp);
flag = 1;
}
}
n--;
}
}
void sequence(char* string[],int count)
{
char* temp = NULL;
for (int i = 0; i < count-1; i++) {
for (int j = 0; j < count-1-i; j++) {
if (strcmp(string[j], string[j+1]) > 0) {//或者寫成strcmp(*(string+j), *(string+j+1))
//char temp[20] = {};
// strcpy(temp, *(string+j));
// strcpy(*(string+j), *(string+j+1));
// strcpy(*(string+j+1), temp);
temp = string[j];
string[j] = string[j + 1];
string[j + 1] = temp;
}
}
}
for (int i = 0; i < 6; i++) {
printf("%s ",*(string + i));
}
printf("\n");
}
int main(int argc, const char * argv[])
{
/*
指針數(shù)組和數(shù)組指針的用法比較
*/
char* string[6] = {"hello", "nice", "hto", "meet", "you", "hahaha"};
char str[][20] = {"hello", "nice", "hto", "meet", "you", "hahaha"};
char (*myStr)[20] = str;
//char str[6][20] = {};
// int flag = 1, n = 6;
// while (flag) {
// flag = 0;
// for (int i = 0; i < n - 1; i++) {
// if (strcmp(*(string+i),*(string + i + 1)) > 0) {
// char tmp[20];
// strcpy(tmp, *(string+i));
// strcpy(*(string + i), *(string + i + 1));
// strcpy(*(string + i + 1), tmp);
// flag = 1;
// }
// }
// n--;
// }
sequence(string, 6);
sortArray(myStr, 6);
for (int i = 0; i < 6; i++) {
printf("%s ",*(str + i));
}
printf("\n");
return 0;
}