#include <stdio.h>
#include <time.h>
#include <string.h>
char *matrix[10][10];
int main(void) {
int i;
char *list[4];
char *words[20] = {
" c a t ", " c a r ", " b e a r ", " s h i p ",
" m o u s e ", " b e a t l e ", " c o a t ", " n e s t ",
" i c e ", " s u g a r ", " b a c o n ", " f r o w n ",
" s m i l e ", " d e a d ", " f e a t h e r ", " g o a t ",
" h e n "," j e l l y "," k o a l a "," l i p s "
};
int length;
int num;
int k;
int m;
char otherString=0;
char *c;
int j;
int s;
int r;
char test[10];
char *token;
const char *search = " ";
char *empty = "";
int size;
int ans;
int x;
int y;
int pos;
int pos2;
int randRow;
int randColumn;
int chosen[10];
int random;
int d;
int ROWS = 10; // number of rows
int COLUMNS = 10; // number of columns
printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n");
srand(time(NULL));
for (i = 0; i < 4; i++) {
printf( "\n" );
d = 0;
do {
random = (rand() % 20);
list[i] = words[random];
d = 0;
for (j = 0; j < i; j++) {
if (strcmp(words[random], list[j]) == 0)
d = 1;
}
} while (d);
}
token = strtok((words[random]), search);
while (token != NULL) {
length = strlen(words[random]);
for (k = 0; k < length; k++) {
matrix[i][k] = token;
token = strtok(NULL, search);
matrix [i][k] = token;
}
}
for (r = 0; r < 10; r++) {
printf("\n");
for (s = 0; s < 10; s++) {
//ans = strlen(matrix[r][s]);
/* if (ans == 0) {
c = 'A' + (rand() % 26);
matrix[r][s] = c;
}*/
printf("\t%s", matrix[r][s]);
}
}
getchar();
return 0;
}
Basically this program generates 4 random words which cannot be duplicate
strtok
is used to slip the words so that they can be entered char by char into matrix. finally any null chars in the matrix will be replaced with random characters. howeverstrtok
is generating runtime error and I am not sure how to check for a null element ?