Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having a little bit of a problem getting my \"thoughts to code\" in order t

ID: 3771432 • Letter: I

Question

I am having a little bit of a problem getting my "thoughts to code" in order to create a simple program that prompts a user for a series of letters, then the program should read in the letters and print out which dictionary words can be made from the provided letters. The "dictionary" is provided as a text file.

These are the steps in my mind already on how to accomplish the task.

1. Create a program that reads the file and print every line of text from the file (fopen)

2. Add the part where you break each line into words and print every word (strtok)

3. Create the function that completes the array with 26 elements to count every letter in a word

4. Create the part where you ask the group of letters from the user

5. Finally create the function that will compare the 2 arrays (26 elements each) one for the group of letters, and the other one for a word from the dictionary.

THIS IS WHAT IS IN THE FILE (dictionary.txt) THE PROGRAM HAS TO READ FROM:

ape   apple
ball   bill   bull
foot
parrot   peeble
season
zebras   zoo

------------------------------------------------------------------------------------------------

// WHAT I HAVE SO FAR

// Reading and printing the dictionary file
#include <stdio.h>
#define SIZE 99

int main( void )
{
char word[SIZE]; // Word from the dictionary
FILE *cfPtr; // cfPtr = dictionary.txt file pointer

// fopen opens file; exits program if file cannot be opened
if ( ( cfPtr = fopen( "dictionary.txt", "r" ) ) == NULL ) {
puts( "File dictionary.txt could not be opened." );
puts( "Check if the file exists in the same directory as the exe program." );
} // end if
else { // read each word from file
fgets( word, SIZE, cfPtr );

// while not end of file
while ( !feof( cfPtr ) ) {
printf( "%s", word );
fgets( word, SIZE, cfPtr );
} // end while

fclose( cfPtr ); // fclose closes the file
} // end else

return 0;
} // end main

--------------------------------------------------------------------------------

// Using function strtok
#include <stdio.h>
#include <string.h>

#define SIZE 80

int main( void )
{
// initialize array string
char string[SIZE];
char word[SIZE];
char *tokenPtr; // create char pointer

puts( " Enter the sentence: " );
fgets( string, SIZE, stdin );

puts( " Enter a word: " );
scanf("%s", word);

tokenPtr = strtok( string, " " ); // begin tokenizing sentence

// continue tokenizing sentence until tokenPtr becomes NULL
while ( tokenPtr != NULL ) {

if (!strcmp(word, tokenPtr))
printf( "%s : This is the word you are looking for ", tokenPtr )
;
else
printf( "%s ", tokenPtr )
;

tokenPtr = strtok( NULL, " " ); // get next token
} // end while

return 0;
} // end main

Explanation / Answer

#include <stdio.h>
#include <string.h>
#define SIZE 80
#define NUM_WORDS 100

// Function to check if two words are anagrams
int check_anagram(char *word1, char *word2)
{
int i=0;
int len= strlen(word1);
int frequency_letters1[26], frequency_letters2[26];
for(i=0;i<26;i++)
{
frequency_letters1[i]=0;
frequency_letters2[i]=0;
}
for(i=0;i<len;i++)
{
frequency_letters1[word1[i]-'a']++;
}
len = strlen(word2);
for(i=0;i<len;i++)
{
frequency_letters2[word2[i]-'a']++;
}
for(i=0;i<26;i++)
if(frequency_letters2[i]<frequency_letters1[i]) return 0;
return 1;

}
int main()
{
int total_words = 0;
char dictionary_words[NUM_WORDS][SIZE]; //Stores all the words in the dictionary
  
FILE *cfPtr; // cfPtr = dictionary.txt file pointer
// fopen opens file; exits program if file cannot be opened
if ( ( cfPtr = fopen( "dictionary.txt", "r" ) ) == NULL ) {
puts( "File dictionary.txt could not be opened." );
puts( "Check if the file exists in the same directory as the exe program." );
} // end if
else { // read each word from file
char line[SIZE];
// while not end of file
while (fgets(line, SIZE, cfPtr)!=NULL)
{
// printf( "%s", line);
char *tokenPtr = strtok( line, " " );
while (tokenPtr != NULL ) {
strcpy(dictionary_words[total_words],tokenPtr);
// printf("%s ",tokenPtr);
total_words++;
tokenPtr = strtok( NULL, " " ); // get next token
}
}
fclose( cfPtr ); // fclose closes the file
}
puts("Enter a string ");
char find_word[SIZE];
scanf("%s",find_word);
printf("%s ",find_word);
int i=0;
printf("The dictionary words that can be made using the given letters are: ");
for(i=0;i<total_words;i++)
{
if(check_anagram(find_word,dictionary_words[i])==1)
printf("%s ",dictionary_words[i]);
}
return 0;
}