#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_WORDS 1000 #define MAX_LEN 256 void shuffle(char * pword[], int words, int iterations) { int n; if ( words <= 1) return; for (n=0; n<iterations;> { int i1, i2; i1 = rand()%words; i2 = rand()%words; char * tmp = pword[i1]; pword[i1] = pword[i2]; pword[i2] = tmp; } } int main() { int n; char buf[MAX_LEN]; char * pword[MAX_WORDS]; int words = 0; FILE * fp = fopen("words.txt", "r"); if ( ! fp ) return -1; while (!feof(fp)) { if (words == MAX_WORDS) break; if (! fgets(buf, MAX_LEN, fp) ) break; buf[strlen(buf)-1] = '\0'; // remove newline pword[words] = strdup(buf); // this dynamically allocates memory words++; } fclose(fp); shuffle(pword, words, 2*words); for (n=0; n<words;> printf("%s\n", pword[n]); // cleanup for (n=0; n<words;> free( pword[n]); return 0; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)