#include <stdio.h> #define NSTRS 10 /* number of strings */ #define STRLEN 16 /* length of each string */ char strs[NSTRS][STRLEN]; /* array of strings */ main() { int i; extern int compare1(), compare2(); /* * Prompt the user for NSTRS strings. */ for (i = 0; i < NSTRS; i++) { printf("Enter string #%d: ", i); gets(strs[i]); } /* * Sort the strings into ascending order. There * are NSTRS array elements, each one is STRLEN * characters long. Note we give the size of * the array element, not the length of the * string in it. */ qsort(strs, NSTRS, STRLEN, compare1); /* * Print the strings. */ printf("\nSorted in ascending order:\n"); for (i = 0; i < NSTRS; i++) printf("\t%s\n", strs[i]); /* * Now sort the strings in descending order. */ qsort(strs, NSTRS, STRLEN, compare2); printf("\nSorted in descending order:\n"); for (i = 0; i < NSTRS; i++) printf("\t%s\n", strs[i]); exit(0); } /* * compare1--compare a and b, and return less than, * greater than, or equal to zero. Since * we are comparing character strings, we * can just use strcmp to do the work for us. */ compare1(a, b) char *a, *b; { return(strcmp(a, b)); } /* * compare2--this compares a and b, but is used for * sorting in the opposite order. Thus it * returns the opposite of strcmp. We can * simulate this by simply reversing the * arguments when we call strcmp. */ compare2(a, b) char *a, *b; { return(strcmp(b, a)); }
file: /Techref/language/ccpp/cref/CONTRIB/OR_USING_C/14.2.c, 1KB, , updated: 1995/2/5 07:18, local time: 2024/11/20 03:42,
18.225.55.210:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/ccpp/cref/CONTRIB/OR_USING_C/14.2.c"> language ccpp cref CONTRIB OR_USING_C 14</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.