 |
 |
translating spanish to english
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
I have to write a program in C that translates from spanish to english using char *foreign[100], *english[100] using 100 words. Now it actually doesnt have to translate it, it just has to pick the correct word with botht the english and spanish already in the program like if the user types in perro(with no articles) then it will give dog. I just need a way to start this program. Any tips would be helpful. Thanks.
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Hi again mindwaves,
I guess that this is another programming assignment for your CS course. From your recent making a calculator assignment, I assume that you're already familiar with reading keyboard-input from the user via 'scanf', etc.? (There are also additional examples of getting user-input in the somewhat alarmingly-named C Programmers NEEDED desperately!! Will pay MONEY!!! thread in this Forum.)
As you know, you could populate the two arrays of strings by referring to any Spanish/English dictionary, such as the online one suggested by Blobby above. You would use the same index-number in the two arrays for each pair of related English/Spanish strings. When the arrays have been populated, you could then use a 'for' loop to search the appropriate array for the input string, matching via 'strcmp' or whatever. Once you've got the index of the matched string, it would be easy to retrieve the translated string from the other array (at the same index).
Of course, if you are allowed to use more efficient search-methods (e.g., binary search), it would be better to write the assignment using one of those, rather than using a relatively slow for-loop that searches linearly through a long array.
Regards,
--Paul
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Thanks for your help. I am familiar with scanf but I am not too sure about strings and string commands. Do you suggest that I have two files one with spanish and the other with the english translation so that #1 in spanish corresonds with the #1 in english in the list. I know that I have to read the file usind fopen I believe but can you please elucidate on strcmp. Thanks.
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Thanks for your help. I am familiar with scanf but I am not too sure about strings and string commands. Do you suggest that I have two files one with spanish and the other with the english translation so that #1 in spanish corresonds with the #1 in english in the list. I know that I have to read the file usind fopen I believe but can you please elucidate on strcmp. Thanks.
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Hi again,
Yes, I was thinking that you would use two "half-dictionary" arrays (one per language), each containing the same number of string (word) elements. [As you know, in 'C', a string is just an array of characters terminated by a NULL. So, each half-dictionary array would actually be a kind of super-array of null-terminated character arrays.] In your case, one of the half-dictionary arrays would hold all the English strings, and the other one would hold all the Spanish strings. (You could also use a single dictionary array to hold "pairs" of Spanish/English strings, but I think it's easier to test with two separate arrays. It doesn't really matter.)
For testing purposes, you probably don't even have to bother with reading in the strings from a file yet; you could just "hard code" the dictionary entries directly into your program. Then, you'd loop through each of the user's input words, checking to see if it's one that's in the appropriate array. If it is, you'd display the corresponding string from the other array; otherwise, you'd just display an error message, and ask the user to try again. Once you're confident that the searching/matching routine is working well, you could then add the code for reading in the dictionary entries from a file.
BTW, the 'strcmp' routine (in <string.h> ) takes two strings, and compares them for equality. The main "tricky" thing about 'strcmp' is that it returns 0 if the strings match; so, when 'strcmp' is used in an 'if' test-condition, you'd usually negate ("flip") its returned value to make it seem as if it returned "true" instead. It's also a case-sensitive routine, althoough some compilers/platforms offer a special version ('stricmp' or '_stricmp') that isn't.
For example, a small program to read in the user's Spanish words, and translate them into English, might look something like the following (however, note that this is a severely limited example in several ways, e.g., it has no subroutines, it handles only two hard-coded word-pairs, it uses a slow for-loop to search for a matching string, etc., etc.):-
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const The_Max_Num_Words = 2; /* We're only testing two word-pairs, for now. */
const The_Max_Word_Length = 21; /* Each word will be 20 letters, plus a terminating NULL */
/* Define the two "half-dictionary" arrays */
char * theEnglishWords[The_Max_Num_Words];
char * theSpanishWords[The_Max_Num_Words];
/* Main dictionary routine */
int main( void ) {
int theFoundIndex; /* The index of a word in a half-dictionary array */
int i; /* A loop counter */
char theQueryWord[The_Max_Word_Length]; /* A place-holder for the user's input word */
/* Populate the two half-dictionary arrays */
/* NOTE: There's no need to allocate memory for the strings, we'll just */
/* let each element point to a static compiler-generated string. */
theEnglishWords[0] = "dog";
theEnglishWords[1] = "cat";
theSpanishWords[0] = "perro";
theSpanishWords[1] = "gato";
/* Prompt for the user's first word */
printf( "\n%s", "Please enter a Spanish word (or, enter . to exit): " );
/* Keep displaying the translations, and prompting for the next input words, until done. */
/* NOTE: The format param's length-field (here 20) should be ('The_Max_Word_Length' - 1). */
while ( scanf( "%20s", theQueryWord ) ) {
/* Check whether the user wishes to stop */
/* NOTE: 'strcmp' returns 0 if strings are equal */
if ( ! strcmp( theQueryWord, "." ) ) break;
/* Echo the user's input word, to provide feedback */
printf( "\n%s '%s'", "You entered the Spanish word:", theQueryWord );
/* Try to find the translation for the user's specified word */
/* Loop through all the half-dictionary words, trying to find the query word */
theFoundIndex = -1; /* Assume failure, initially */
for ( i = 0; i < The_Max_Num_Words; i++ ) {
/* NOTE: 'strcmp' returns 0 if strings are equal, and is case-sensitive */
if ( ! strcmp( theSpanishWords[i], theQueryWord ) ) {
/* We found the query word */
theFoundIndex = i;
break;
}
}
/* Display the translation, or indicate failure */
if ( theFoundIndex > -1 ) {
printf( "\n%s '%s'", "The English translation is :",
theEnglishWords[theFoundIndex] );
} else {
printf( "\n%s", "Sorry, I don't know that word. Please try again." );
}
/* Prompt for the next input word */
printf( "\n\n%s", "Please enter a Spanish word (or, enter . to exit): " );;
}
/* Exit */
printf( "\n%s", "Thank you for using the dictionary. Goodbye!" );
return 0;
}
Please let us know if you have any issues with 'strcmp', etc. I wish you the best of luck with your assignment!
Regards,
--Paul
[This message has been edited by Paul Crawford (edited 12-01-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Thank you very much again. I appreciate you taking the time to write the program and I am analyzing it right now to see how it works. Thanks again.
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|