Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIDTH 50
int main()
{
char **wordarr, **wordpc;
//char sentence[1000];
int option, library, howmany, i,number, numwords=0,count=0;
printf("1. Dictionary build\n2. Lets play\n(pls. make a choice 1-2, 0 for exit):\n");
scanf("%d", &option);
while(option != 0){
//open the file
FILE *fp;
fp = fopen("word.txt", "r");
if (fp == NULL) {
fprintf(stderr, "File not found\n");
exit(1);
}
//if he wants to add more words
/*printf("\nDo you want to add more words?(y/n):");
char opt;
scanf("%s", &opt);
if(opt == 'y'){
printf("add the words with a space between them:\n");
gets (sentence);
fp = fopen("word.txt", "a");
fprintf(fp, " %s", sentence);
fclose(fp);
}*/
//counts the words in the file
int ch, letterCount = 0;
while((ch = fgetc(fp)) != EOF){
if(ch == ' ' || ch == '\n' ){
count++;
}
else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
else
letterCount +=1;
}
printf("\nletterCount %d\ncount %d\n", letterCount, count);
//making the array
wordarr = (char**)malloc(sizeof(char*) * count+1);//count+1 giati afhnei sunexeia mia le3h xwris na thn metraei
for(i = 0; i<count+1; i++){
wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
}
//
//putting the words into the array
char buffer[10000];
fp = fopen("word.txt", "r");
char *tok;
i=0;
int d;
while( fgets(buffer, 10000, fp) != NULL){
int len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n') {
buffer[--len] = '\0';
}
tok = strtok(buffer, " .,!");
while (tok != NULL) {
int len = strlen(tok);
for ( d = 0; d < len; d++){
strcpy(wordarr[i], tok);
}
i++;
tok = strtok(NULL, " .,!");
}
}
fclose(fp);
int j=0;
for (j=0; j<i; j++) {
printf("[%s]\n", wordarr[j]);
}
printf("\nWrite how many letters does the word have?\n");
scanf("%d", &number);
//goes to the first array and searches for words with NUM characters
//stores the words to an array
//make a static so it can hold the position of where the word is so
//I can copy it to the other array
int stat_position[20];
int position = 0,len, size=count+1, oneword = 0;
for(i=0; i<size; i++)
{
len=0;
len = strlen(wordarr[i]);
if( len == number){
numwords++;
if(numwords == 1){oneword= i; }
printf("\n%d\n", numwords);
//only move that int array element by one if len found
// to keep what element number the wordarr is inside of it
// and not put the proper number element of your wordarr
// in the same int element number
stat_position[position]=i;
printf("position %d word is %s\n", position, wordarr[i]);
position++;
}
}
/**IF THE NUMBER OF WORDS EQUAL TO ONLY ONE WORD IN THE ARRAY THEN PRINTED OUT AND TERMINATE THE GAME*/
if(numwords == 1){
printf("\nComputer won:%s\n", wordarr[oneword]);
exit(0);
}
//ALLOCATE 2DARRAY
wordpc = (char**)malloc(sizeof(char*) * numwords);
for(i = 0; i<numwords; i++){
wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
}
printf("\n");
// now you have your int array with the proper
// amount of elelments holding the proper number
// of which element number wordarr has the
// chosen words in it by length
for ( i = 0; i < position; i++){//MATCHING WORDS COPIED TO THE second array
strcpy(wordpc[i], wordarr[stat_position[i]]);
}
//Frequency of the characters
int freq[26];//array holding the frequency of the characters
for(i=0; i<26; i++)
{
freq[i] = 0;
}
char alpha[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char str[WIDTH];
j=0;
int k;
while( j < numwords){
strcpy(str, wordpc[j]);
//printf("%s", str);
i=0;
while (str[i] != '\0'){
for( k=0; k<26; k++ ){
if (str[i] == alpha[k] ){
freq[k]++;
}
}
i++;
}
j++;
}
for (i = 0; i < 26; i++)
{
/** Printing only those characters
whose count is at least 1 */
if (freq[i] != 0)
printf("\n%c occurs %d times in the entered string.\n", alpha[i],freq[i]);
}
/** Bubble Sort*/
int swap;
char t;
for (i = 0 ; i < ( 26 - 1 ); i++)
{
for (j = 0 ; j < 26 - i - 1; j++)
{
if (freq[j] < freq[j+1]){
swap = freq[j];
freq[j] = freq[j+1];
freq[j+1] = swap;
t = alpha[j];
alpha[j] = alpha[j+1];
alpha[j+1] = t;
}
}
}
for ( i = 0 ; i < 26 ; i++ )
printf("%d %c\n", freq[i], alpha[i]);
//Print out the character with the biggest frequency //ean 3erw gw ftasei na exei akoma kai ena swsto na 8umatai to swsto kai na 3ekinaei apo kei
i=0,j=0,k=0;
char tempword[numwords];
int tries=0, correct_answers=0;
printf("Suggesting character: %c\n", alpha[i]);
tries++;
while( i < numwords){
strcpy(str, wordpc[i]);
printf("\n%s\n", str);
while(str[j] == alpha[j]){
//strcpy(tempword[k], alpha[j]);
printf("\n%c", alpha[j]);
printf("\nCorrect character.go for the next one:");
correct_answers++;
j++;
tries++;
if(tries == 6 ){
printf("\nComputer lost. You Won\n");
exit(0);
}
}
printf("\n%c", alpha[j]);
j++;
tries++;
if(tries == 6 ){
printf("\nComputer lost. You Won\n");
exit(0);
}
printf("\nWrong character.guess again: ");
i++;
}
}
//Deallocate the memory
for (i=0; i<count; i++) {
free(wordarr[i]);
free(wordpc[i]);
}
free(wordarr);
free(wordpc);
return 0;
}