int main(void) { char phrase[MAX_PHRASE_LENGTH]; char encoded[MAX_PHRASE_LENGTH];
printf("Enter phrase to encode: "); gets(phrase);
EncodePhrase(phrase, encoded);
printf("Encoded: %s\n\n", encoded);
return 0; }
void EncodePhrase(char *original, char *encoded) { char c; int i = 0;
/* use while loop to read through the original string and store the converted character into encoded array */ while (original[i] != '\0'){ c = original[i]; encoded[i] = EncodeCharacter(c); i++; /* add a NULL pointer at the end of the encoded string */ encoded[i] = '\0'; } }
char EncodeCharacter(char c) { char character; int value;
/* convert character into ascii code */ value = (int)c;
/* only change ascii value if it is alphabet */ if ((value >= 97) && (value <= 109)){ value+=13; } else if ((value >= 110) && (value <= 122)){ value-=13; } else if ((value >= 65) && (value <= 77)){ value+=13; } else if ((value >= 78) && (value <= 90)){ value-=13; }
/* convert ascii code into character */ character = (char)value;
/* return the encoded character */ return character; }