In: Computer Science
C Code! I need all of the \*TODO*\ sections of this code completed.
For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words and print out the corresponding French word, at the same index i.
#include <stdio.h> #include <string.h> const int NUMBER_ENTRIES = 352; const char* english_NUMBER_ENTRIES[5]= {"war","size", "her","song","time"}; const char* french_NUMBER_ENTRIES[5] = {"guerre","taille","sa", "chanson","temps"}; int main(int argc, char **argv) { const int BUFFER_LENGTH = 128; char c, t; char user_word[BUFFER_LENGTH]; enum { ENGLISH_FRENCH, FRENCH_ENGLISH } direction; int i; /* TODO: Add additional declarations here */ printf("Dictionary\n\n"); printf("English-French Dictionary\n\n"); do { printf("Enter e for English-French\n"); printf("Enter f for French-English\n"); scanf("%c", &c); scanf("%c", &t); } while (!((c == 'e') || (c == 'f'))); switch (c) { case 'e': direction = ENGLISH_FRENCH; break; case 'f': direction = FRENCH_ENGLISH; break; } printf("Please enter a word in "); switch (direction) { case ENGLISH_FRENCH: printf("English"); break; case FRENCH_ENGLISH: printf("French"); break; } printf(": "); i = 0; scanf("%c", &c); while (c != '\n') { user_word[i] = c; i++; if (i >= BUFFER_LENGTH) { i = BUFFER_LENGTH-1; } scanf("%c", &c); } user_word[i] = '\0'; switch (direction) { case ENGLISH_FRENCH: printf(" The corresponding word is: "); /* TODO */ break; case FRENCH_ENGLISH: printf(" The corresponding word is: "); /* TODO */ break; } return 0; }
Code :
#include <stdio.h>
#include <string.h>
const int NUMBER_ENTRIES = 352;
const char* english_NUMBER_ENTRIES[5]= {"war","size", "her","song","time"};
const char* french_NUMBER_ENTRIES[5] = {"guerre","taille","sa", "chanson","temps"};
int main(int argc, char **argv)
{
const int BUFFER_LENGTH = 128;
char c, t;
char user_word[BUFFER_LENGTH];
enum { ENGLISH_FRENCH, FRENCH_ENGLISH } direction;
int i;
/* TODO: Add additional declarations here */
printf("Dictionary\n\n");
printf("English-French Dictionary\n\n");
do {
printf("Enter e for English-French\n");
printf("Enter f for French-English\n");
scanf("%c", &c);
scanf("%c", &t);
} while (!((c == 'e') || (c == 'f')));
switch (c) {
case 'e':
direction = ENGLISH_FRENCH;
break;
case 'f':
direction = FRENCH_ENGLISH;
break;
}
printf("Please enter a word in ");
switch (direction) {
case ENGLISH_FRENCH:
printf("English");
break;
case FRENCH_ENGLISH:
printf("French");
break;
}
printf(": ");
i = 0;
scanf("%c", &c);
while (c != '\n') {
user_word[i] = c;
i++;
if (i >= BUFFER_LENGTH) {
i = BUFFER_LENGTH-1;
}
scanf("%c", &c);
}
user_word[i] = '\0';
int size = sizeof(english_NUMBER_ENTRIES)/sizeof(english_NUMBER_ENTRIES[0]);
int index;
switch (direction) {
case ENGLISH_FRENCH:
for(int i = 0; i<size; i++)
{
if(strcmp(english_NUMBER_ENTRIES[i], user_word) == 0) // strcmp is 0 if values are same
{
index = i; // get the index
}
}
printf(" The corresponding word is: ");
printf("%s\n", french_NUMBER_ENTRIES[index]);
break;
case FRENCH_ENGLISH:
for(int i = 0; i<size; i++)
{
if(strcmp(french_NUMBER_ENTRIES[i], user_word) == 0) // strcmp is 0 if values are same
{
index = i; // get the index
}
}
printf(" The corresponding word is: ");
printf("%s\n",english_NUMBER_ENTRIES[index]);
break;
}
return 0;
}
Variation in your code that i have done is :
OUTPUT :