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

Write a program that prompts the user to enter the name of a file for the conten

ID: 674876 • Letter: W

Question

Write a program that prompts the user to enter the name of a file for the content to be shifted:

1. The program reads the content of the file and shifts the content by the shift amount, then writes the shifted message to a file with the same name but an added extension
of .sft. In this example, the original file name is jennys_message.txt, so the shifted message will be stored in a file named jennys_message.txt.sft. Assume the file name is no more than 100 characters. Assume the length of each line in the input file is no more than 10000 characters.

2. The program should include the following function:

The function expects message to point to a string containing the message to be shifted; shift_amount represents the amount by which each letter in the message to be shifted. Lower-case letters remain lower-case when shifted, and upper-case remain upper-case. For example, if the message is “Go ahead, make my day.”, and shift_amount is 3, the function will modify message to “Jr dkhdg,

pdnh pb gdb.” If the message is “Jr dkhdg, pdnh pb gdb.”, and shift_amount is 23, the function will modify message to “Go ahead, make my day.”.

Hint: To handle the wrap-around problem, use the % operator. For example, ‘X’ shifted by 4 is ‘B’.

Explanation / Answer

#include #include #include #include #include #include char num(char c) { const char upper_alph[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; const char lower_alph[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; if(isupper(c)) { for(int i = 0; i < 26; i++) if(upper_alph[i] == c) return i; } else { for(int i = 0; i < 26; i++) if(lower_alph[i] == c) return i; } return 0; } void encrypt(char *message, int shift) { int i = 0; const char upper_alph[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; const char lower_alph[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; while(message[i] != NULL) { if(isalpha(message[i])) { if(isupper(message[i])) { printf("%c", upper_alph[(num(message[i])+shift)%26]); } else { printf("%c", lower_alph[(num(message[i])+shift)%26]); } } else { printf("%c", message[i]); } i++; } } #define OK 0 #define NO_INPUT 1 #define TOO_LONG 2 static int getLine (char *prmpt, char *buff, size_t sz) { int ch, extra; // Get line with buffer overrun protection. if (prmpt != NULL) { printf ("%s", prmpt); fflush (stdout); } if (fgets (buff, sz, stdin) == NULL) return NO_INPUT; // If it was too long, there'll be no newline. In that case, we flush // to end of line so that excess doesn't affect the next call. if (buff[strlen(buff)-1] != ' ') { extra = 0; while (((ch = getchar()) != ' ') && (ch != EOF)) extra = 1; return (extra == 1) ? TOO_LONG : OK; } // Otherwise remove newline and give string back to caller. buff[strlen(buff)-1] = ''; return OK; } int main() { //reverse(); //printf(" "); int rc; char mes[1024]; int sh = 0; rc = getLine ("Enter message to be encrypted: ", mes, sizeof(mes)); if (rc == NO_INPUT) { // Extra NL since my system doesn't output that on EOF. printf (" No input "); return 1; } if (rc == TOO_LONG) { printf ("Input too long [%s] ", mes); return 1; } encrypt(mes, 1); fflush(stdin); getchar(); return 0; }