Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Translates a set of english strings into spanish, using a file \"words.dat\" whi

ID: 3534249 • Letter: T

Question

Translates a set of english strings into spanish, using a file "words.dat" which consists of 100sets of words:

"Englishword, spanishword

Engishword, spanishword

etc."

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAXCHAR 26
#define WORDCOUNT 100
#define MAXSTRING 250

int findWord (char english[][MAXCHAR], char target[], int wordCount){//return where target is found or -1 if not found
    int i;

    for(i=0;i<wordCount;i++){
        if(strcmp(english[i],target)==0)
            return i;
    }
    return -1;
}

int main(){
      char english[WORDCOUNT][MAXCHAR];
      char spanish[WORDCOUNT][MAXCHAR];
      char UserStr[MAXCHAR];
      char temp[MAXCHAR];
      char TranslatedStr[MAXSTRING];
      int wordCount=0;
      int index;

      FILE *inFile;
      inFile = fopen("words.dat", "r");

      if(inFile == NULL){
          printf("File could not be opened.");
          exit(-1);
      }

      //get data from file
      while(!feof(inFile) && wordCount<WORDCOUNT){
            fscanf(inFile,"%[^,]s",english[wordCount]);
            fgetc(inFile); //kills the ,
            fscanf(inFile,"%s",spanish[wordCount]);
            fgetc(inFile); //kills the

            wordCount++;
      }
      fclose(inFile);

      //loop
      while (1){
          printf ("Enter a word, phrase in quotes, or 'quit' to quit: ");
          scanf("%s",UserStr);//get user string
          if( strcmp(UserStr,"quit")==0) //break from while loop if 'quit' entered
             break;
          // check if first character is " if it isn't proces the 1 word
          if( UserStr[0] != '"'){
              memcpy(UserStr, UserStr+1, strlen(UserStr));
              index=findWord(english,UserStr,wordCount);//get index where UserStr is found
              if(index==-1)
                  strcpy(TranslatedStr, "***** ");
              else{
                  strcpy(TranslatedStr, spanish[index]);
                  strcat(TranslatedStr, " "); //add space
              }
          }
          //keep getting strings till last "
          while (1){
              scanf ("%s", UserStr);//get user string
              //final word
              if(UserStr[strlen(UserStr)-1] == '"'){
                  UserStr[strlen(UserStr)-1] = '';
                  index=findWord(english,UserStr,wordCount);//get index where UserStr is found
                  if(index==-1)
                      strcat(TranslatedStr, "***** ");
                  else
                      strcat(TranslatedStr, spanish[index]);
                  break;//Final word translated break out of loop
              }
              index = findWord (english,UserStr,wordCount);//get index where UserStr is found
              if(index==-1)
                  strcat(TranslatedStr, "***** ");
              else{
                  strcat(TranslatedStr, spanish [index]);
                  strcat(TranslatedStr, " "); //add space
              }
          }
          printf("%s ", TranslatedStr);
      }
// }//end while 1

printf("Press any key to continue: ");
getchar();
return 0;
}//end of main

Explanation / Answer

This looks like my code from awhile back that i threw together. What's the problem that you're having with it?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote