#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 08:18, local time: 2024/8/23 05:42,
18.116.90.81: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://piclist.com/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? |
![]() o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
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! |
.