Write a program which drills a user in English - French vocabulary. Two arrays, english and french are used to hold the words such that french[i] contains the French equivalent of the word in english[i]. The program must display an English word and requests the user to enter the French equivalent for it. Incorporate a suitable scoring mechanism and give the user the opportunity to "give-up". Modify the program to enable the user choose either English - French drill or French - English drill.

Source Code

Brief explanation is provided after the source code.

 
#include <stdio.h>
#include <string.h>
#define WORDS 10
#define WORDLENGTH 50

int main(int argc, char ** argv) {
    int i = 0, option, score = 0, drill;
    char *buf[WORDLENGTH], **dic, **eqv;
    char *en[][WORDLENGTH] = {"Morning", "Evening", "Dinner", "Door", "Doctor", "Teacher", "Year", "Towel", "Team", "Truck"};

    char *fr[][WORDLENGTH] = {"Matin", "Soiree", "Diner", "Porte", "Docteur", "Professeur", "Annee", "Serviette", "equipe", "Camion"};

    printf("Welcome. Choose a drill\n");
    printf("1. English - French\n");
    printf("2. French - English\n");
    scanf("%d", &drill);
    while(drill != 1 && drill != 2) {
        printf("Select a drill. Press 1 for English - French drill and 2 for French - English drill\n");
        scanf("%d", &drill);
    }

    if (drill == 1) {
        printf("Enter the french equivalent of the following words:\n");
        dic = en;
        eqv = fr;

    } else {
        printf("Enter the english equivalent of the following words:\n");
        dic = fr;
        eqv = en;
    }

    L: while (i < WORDS) {

        printf("%d. %s\n", i+1, *(dic + i));
        scanf("%s", &buf);

        if (strcasecmp(buf, *(eqv + i)) == 0) {
            printf("Correct\n");
            score++;
        } else {
            printf("Wrong\n");
            printf("Press 0 to try again\n");
            printf("Press 1 to skip to next\n");
            printf("Press 2 to quit\n");
            scanf("%d", &option);
            if (option == 0) {
                 goto L;
            }
            if (option == 2) {
                break;
            }
        }
        i++;
    }
     printf("Your score is %d/%d.", score, WORDS);
     if (score < WORDS)
        printf("\nTo get %d/%d, you must complete the drill by correctly translating %d different words\n", WORDS, WORDS, WORDS);
     else
        printf("\nCongratulation. You had all answers correct\n");
    return 0;

}

When you compile and execute the above program it produces the following result on Linux:

 
Welcome. Choose a drill
1. English - French
2. French - English
1
Enter the french equivalent of the following words:
1. Morning
Matin
Correct
2. Evening
Soiree
Correct
3. Dinner
Dine
Wrong
Press 0 to try again
Press 1 to skip to next
Press 2 to quit
0
3. Dinner
Diner
Correct
4. Door
Porte
Correct
5. Doctor
Docteur
Correct
6. Teacher
Professeur
Correct
7. Year
Ane
Wrong
Press 0 to try again
Press 1 to skip to next
Press 2 to quit
0
7. Year
Annee
Correct
8. Towel
Serviette
Correct
9. Team
EQUIPE
Correct
10. Truck
Camion
Correct
Your score is 10/10.
Congratulation. You had all answers correct

Brief Explanation

  • The program starts by initializing two arrays en and fr with French and English words respectively.
  • Next, the user is requested to choose a drill by entering 1 for English – French drill and 2 for French – English drill.
  • Using the while loop, the program validates that either 1 or 2 is inputted by the user. If a number other than 1 or 2 is entered, the program would keep asking the user to select the right drill.
  • If 1 is inputted i.e English – French drill selected, the array containing the English words would be assigned to variable dic and the array containing the French equivalent would be assigned to variable eqv and vice versa.
  • Using the while loop, the program loops through each word in dic and request the user to input either English or French equivalent depending on drill selected.
  • The word inputted by the user is compared with the word in eqv, the array holding the equivalent, and if they match, then the user got it right. The function strcasecmp is used to achieve this. The strcasecmp() function compares string1 and string2 without sensitivity to case. All alphabetic characters in string1 and string2 are converted to lowercase before comparison. The strcasecmp() function operates on null terminated strings. The string arguments to the function are expected to contain a null character ('\0') marking the end of the string. The strcasecmp() function returns a value indicating the relationship between the two strings, as follows: Less than 0 (string1 less than string2), 0 (string1 equivalent to string2) Greater than 0 (string1 greater than string2)
  • If the word entered by the user is wrong, the program would give him the opportunity to try again, skip or quit by entering 0, 1 or 2 respectively.
  • The user final score is displayed to the stdout using the printf function.

Add comment


Security code
Refresh