fork(1) download
  1. #include <string.h>
  2.  
  3. //Try not to hardcode values (like '50', '20', or '49')
  4. //Instead, put them into constants so if you ever change the value,
  5. //you won't accidentally miss any usages.
  6. const int WordSize = 32;
  7.  
  8. //Values that belong together should stay together, preferably by putting them in a structure.
  9. typedef struct MnemonicWordStruct
  10. {
  11. char originalWord[32];
  12. char memorizedWord[32];
  13. } MnemonicWord;
  14.  
  15. const char *GetWordFromMnemonic(const char *memorizedWord, MnemonicWord *mnemonicWords, int numMnemonicWords)
  16. { // Don't forget the opening bracket!
  17.  
  18. //Iterate over each mnemonic work, looking for a match.
  19. int i;
  20. for(i = 0; i < numMnemonicWords; ++i)
  21. {
  22. //Compare this mnemonic's memorized word to the memorized word we passed into the function.
  23. if(strcmp(mnemonicWords[i].memorizedWord, memorizedWord) == 0)
  24. {
  25. //If it matches, return the actual word.
  26. return mnemonicWords[i].originalWord;
  27. }
  28. }
  29.  
  30. //If we haven't found a match, return NULL.
  31. return NULL;
  32. }
  33.  
  34. int main()
  35. {
  36. const int NumMnemonicWords = 6;
  37. MnemonicWord mnemonicWords[6];
  38.  
  39. strcpy(mnemonicWords[0].originalWord, "Paranthesis");
  40. strcpy(mnemonicWords[0].memorizedWord, "Please");
  41.  
  42. strcpy(mnemonicWords[1].originalWord, "Exponents");
  43. strcpy(mnemonicWords[1].memorizedWord, "Excuse");
  44.  
  45. strcpy(mnemonicWords[2].originalWord, "Multiplication");
  46. strcpy(mnemonicWords[2].memorizedWord, "My");
  47.  
  48. strcpy(mnemonicWords[3].originalWord, "Division");
  49. strcpy(mnemonicWords[3].memorizedWord, "Dear");
  50.  
  51. strcpy(mnemonicWords[4].originalWord, "Addition");
  52. strcpy(mnemonicWords[4].memorizedWord, "Aunt");
  53.  
  54. strcpy(mnemonicWords[5].originalWord, "Subtraction");
  55. strcpy(mnemonicWords[5].memorizedWord, "Sally");
  56.  
  57. int i;
  58. for(i = 0; i < NumMnemonicWords; ++i)
  59. {
  60. const char *lookup = mnemonicWords[i].memorizedWord;
  61. const char *answer = GetWordFromMnemonic(lookup, mnemonicWords, NumMnemonicWords);
  62. printf("%s = %s\n", lookup, answer);
  63. }
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
Please = Paranthesis
Excuse = Exponents
My = Multiplication
Dear = Division
Aunt = Addition
Sally = Subtraction