I enjoy studying foreign languages. A tool like Google Translate comes in handy, but it’s not perfect. That’s because computers translate words and phrases, but not the living, spoken language. Regardless, I thought I’d give language translation a stab, which got me into the topic of exploring arrays.
The easiest parts of speech to translate are nouns. These pair up fairly well between most languages. For example, numbers translate directly between all languages. But for my program, I sought to translate months of the year. The languages chosen are French and English, one of which I speak fairly well.
Before my brain entangled itself in the process of how to input a word from one language and output its foreign tongue complement, I first wanted to output the names of months as they pair up.
2023_06_03-Lesson.c
#include <stdio.h>
#include <string.h>
#define MONTHS 12
int main()
{
const char *english[MONTHS] =
{
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"
};
const char *french[MONTHS] =
{
"janvier", "fevrier", "mars", "avril",
"mai", "juin", "jullet", "aout", "septembre",
"octobre", "novembre", "decembre"
};
int x;
/* output months */
for( x=0; x<MONTHS; x++ )
printf("%s - %s\n", english[x], french[x]);
return(0);
}
The code hosts two arrays, english[]
and french[]
. Both are pointer arrays, each with MONTH
(12) elements in their annual order. These are constant arrays as their values don’t change, nor should they be changed due to the string-pointer declaration. (Relevant post.)
The for loop processes the arrays and outputs the corresponding values:
January - janvier
February - fevrier
March - mars
April - avril
May - mai
June - juin
July - jullet
August - aout
September - septembre
October - octobre
November - novembre
December - decembre
Upon success, I sought to devise a translation function that would output the French month name when given the English month name. That’s where I stopped.
My primitive attempts at translation work only when a link exists between the two arrays. In this Lesson’s code the link exists because each element in each array pairs up with the translated word. In the program I started to write, input is accepted and the offset passed to a function that returns the corresponding word. While this program worked, I found it to be unimpressive. It was a lot of code to do what something else could accomplish a lot easier.
That something else, something that the C language is missing, is called an associative array. I explore this concept in next week’s Lesson.
<pedantic>juillet</pedantic>
Is it that obvious that I don’t speak French?