/* cheat.c by Shawn Nematbakhsh snematbakhsh@cs.ucr.edu * takes a list of letters and a number of wild tiles(0-9) as args, * and prints all words that can be assembled using those tiles - * also the "bombs" are printed at the end, "bombs" being words that * use ALL of the input tiles */ #include #include #include #define MAX_SIZE 100 int main(int argc, char **argv) { int c[26]; int cc[26]; int i; FILE *fp; char word[30]; char *p; int dif; char bombs[MAX_SIZE][30]; int on=0; char *oldArg=argv[1]; for(i=0;i<26;++i) c[i]=0; if(argc != 3) { printf("Check usage\n"); return 0; } while(*argv[1] != '\0') c[*argv[1]++ - 'a']++; if((fp=fopen("dict.txt","r"))==NULL) { printf("Can't open file\n"); return 0; } while((fscanf(fp,"%s\n",word))!= EOF) { p=word; for(i=0;i<26;++i) cc[i]=0; while(*p != '\0') cc[*p++ - 'a']++; if(argv[2][0]=='0') { for(i=0;i<26;++i) { if(cc[i]>c[i]) break; } if(i==26) { printf("%s\n",word); if(strlen(word)==strlen(oldArg)+argv[2][0]-'0') { strcpy(bombs[on++],word); } } } else { dif=0; for(i=0;i<26;++i) { if(cc[i]>c[i]) dif+=cc[i]-c[i]; if(dif>argv[2][0]-'0') break; } if(i==26) { printf("%s\n",word); if(strlen(word)==strlen(oldArg)+argv[2][0]-'0') { strcpy(bombs[on++],word); } } } } fclose(fp); if(on > 0) { printf("\nBombs:\n\n"); for(i=0;i