In: Computer Science
Using a text editor, create a file english.txt containing the following words: yes no go come thanks goodbye Create a second file chinese.txt contain the following words: shi bu qu lai xiexie zaijian Write a program that reads the words from english.txt so that the successive slots of a char * array english point to them. Use the %s conversion code in a fscanf call to read one word at a time. Similarly, read the words from chinese.txt so that the successive slots of a char * array chinese point to them. Your program should then execute a loop that prompts the user for an English word, reads it in (use scanf), and then displays the corresponding Chinese word. The loop should continue until the user enters the word "done".
If you have any doubts, please give me comment...
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 100
int main(){
char english_words[MAX_SIZE][50], chinese_words[MAX_SIZE][50];
char word[50];
int n, i, j;
FILE *eng_fp, *chinese_fp;
eng_fp = fopen("english.txt", "r");
if(!eng_fp){
printf("Unable to open english.txt\n");
return -1;
}
chinese_fp = fopen("chinese.txt", "r");
if(!chinese_fp){
printf("Unable to open chinese.txt\n");
return -1;
}
n = 0;
while(!feof(eng_fp)){
fscanf(eng_fp, "%s", english_words[n]);
n++;
}
n = 0;
while(!feof(chinese_fp)){
fscanf(chinese_fp, "%s", chinese_words[n]);
n++;
}
fclose(chinese_fp);
fclose(eng_fp);
printf("Enter english word (enter 'done' to terminate): ");
scanf("%s", word);
while(strcmp(word, "done")!=0){
int found = 0;
for(i=0; i<n; i++){
if(strcmp(english_words[i], word)==0){
found=1;
break;
}
}
if(found){
printf("%s corresponding chinese word is: %s\n", word, chinese_words[i]);
}
else{
printf("English word %s not found!\n", word);
}
printf("\nEnter english word (enter 'done' to terminate): ");
scanf("%s", word);
}
return 0;
}