/* ================================================================== * * CS 160, Fall 2005 (Beck * * ARM Lab 2 --- Word Parsing [SOLUTION] * * wordmain.c * * Driver program that populates lists to be manipulated/sorted by * three external ARM assembly routines (found in 'asmsort.s'). * * ================================================================== */ #include #include #include #include // Number of input bytes... #define N 100 // Reference external ARM assembly routines... extern void genwords(char *A, char **B); extern int wordgt(char *A, char *B); extern void sortwords(char **B); int main (int argc, char *argv[]) { char *listS, **listR, **p; int i; // Allocate memory for two lists... listS = (char *) malloc(N * sizeof(char)); listR = (char **) malloc(N * sizeof(char *)); // Create input list strcpy(listS, "how much wood can a wood chuck canoodle "); // Call genwords... genwords(listS, listR); // Print the list of words for (p = listR; *p; p++) printf("%s\n", *p); printf("\n"); // Call sortwords... sortwords(listR); // Print the list of words after sortwords for (p = listR; *p; p++) printf("%s\n", *p); printf("\n"); // Free memory allocated for the lists... free(listS); free(listR); return 0; } void genwords(char *S, char **R) { char *p = S; char **q = R; while (*p) { *(q++) = p; while (*p != ' ') p++; *p = 0; p++; } *q = 0; } int wordgt(char *A, char *B) { char *pa = A; char *pb = B; while (*pa == *pb) {pa++; pb++;} if (*pa > *pb) return 1; else return 0; } void sortwords(char **list) { int j, k; char *tmp; for (j=0; j0; j=j-1){ for (k=j-1; k>=0; k=k-1) { if (wordgt(list[k], list[j])) { tmp = list[k]; list[k] = list[j]; list[j] = tmp; } } } }