If you are going to use strcpy
, you have to use it thoughout:
for (int j = 1; j < m; j++) {
int i = j - 1;
char temp[MAX_LEN];
strcpy(temp, arrayIn[j]);
while (i >= 0 && strcmp (arrayIn[i], temp) < 0) {
strcpy(arrayIn[i+1], arrayIn[i]);
i = i - 1;
}
strcpy(arrayIn[i+1], temp);
}
This works if your array is an array of char arrays as in your case. If you have an array of pointers to char, you can write your sort with assignments just as for integers:
for (int j = 1; j < m; j++) {
int i = j - 1;
char *temp;
temp = arrayIn[j];
while (i >= 0 && strcmp(arrayIn[i], temp) < 0) {
arrayIn[i+1] = arrayIn[i];
i = i - 1;
}
arrayIn[i+1] = temp;
}
The sorting condition must still be checked with strcmp
, however. Note that in the second case, the pointers may point to constant immutable strings.