C Code! dictionary.c, start by downloading the boilerplate1 code from Blackboard.
Read the code, then complete it at all places indicated by TODO.
For this second assignment, you do not receive precise specifications. This is on purpose: in the software industry, you’ll find a lot of cases when you need to finish code started by others, while not being given precise instructions on how the code works or is supposed to work. The comments in the code will help you, but you will have to do some detective work with some examples for which you run the code “by hand” in your mind to complete the code.
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. There are smart ways and less smart ways to look up a word in a sorted array. For this assignment, these different algorithmic ways do not matter. 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.
You may use the strcmp function available when including string.h.
Here are a couple of example runs of the complete program:
English-French Dictionary Enter e for English-French Enter f for French-English e
Please enter a word in English: time The English word "time" corresponds to the French word "temps".
Here's the code! you don't need to enter in all of the french and english words, just figure out the program that searches for the words and what they correspond to--main code at the bottom
#include <stdio.h> #include <string.h> const int NUMBER_ENTRIES = 352; const char* english[NUMBER_ENTRIES] = {"war", "size", "her", "song" }; const char* french[NUMBER_ENTRIES] = { "guerre", "taille", "sa", "chanson" }; 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("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: /* TODO */ break; case FRENCH_ENGLISH: /* TODO */ break; } return 0; }
C program to find corresponding words in English-French and verse versa.
Source 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","temp"};
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;
}
The Output
Get Answers For Free
Most questions answered within 1 hours.