Joined: Sun May 25, 2008 5:34 pm Posts: 95 Has thanked: 2 time Have thanks: 1 time
Code:
#include <stdio.h> #include <string.h>
/* * Program to read a list of strings from standard input and print them in * sorted order. This uses a simple selection sort. */
#define MAX_STRING_SPACE 1000 #define MAX_NUM_STRINGS 250 #define MAX_STRING_SIZE 50 int main(void) { /* Where the general string contents are stored. */ char string_space[MAX_STRING_SPACE];
/* The start of each string. */ char *strings[MAX_NUM_STRINGS];
/* Read the strings. */ char buf[MAX_STRING_SIZE]; char *next_space = string_space; int inloc = 0; while(scanf("%s", buf) == 1) { /* Find the length of the string and see if it fits. */ int length = strlen(buf) + 1; if(next_space + length >= string_space + MAX_STRING_SPACE) break; if(inloc >= MAX_NUM_STRINGS) break;
/* Place the string into the structure. */ strings[inloc++] = next_space; strcpy(next_space, buf); next_space += length; }