i have an assignment i need help with. so the goal of this is to encode a file using ascii character codes by mapping the characters in the file to different asci codes. example: ascii codes lowercase letters 97-122 will map to ascii codes 33-58. the assignment specifies to achieve this with the switch/case function but i am not sure how to begin. so far i am somewhat able to encode the file but with only if/else if statements. im only asked to encode lowercase,space,period,and spaces, anything else should be printed as is . how should i approach achieving what my code does using a switch statement. i know how to do it with A LOT of case statements but that doesnt seem efficient and the assignment specifically states
"Note: don't include a separate case in the switch( ) function for each
and every possible input/output char value; too many lines of code. Be smart. Lowercase characters have ascii char codes
97-122. For encoding, ascii char codes 97-122 are mapped to ascii char codes 33-58. That is, you subtract 64 from the
ascii char code 97 to obtain the encoded char value 33. Therefore, use the switch( ) function with a single case for the
lowercase characters. Use a single case for encoding the digits 0-9 also. Similarly, for decoding, you can also use a single
case for decoding the lowercase characters and a single case for decoding the digits.
NOTE: the code isnt complete, im more focused on the procedures/methods i should consider not the actual code for an answer...
the right way is probably to make a lookup table of 256 that remaps the characters so all you have to do is
loop over oldstring like this:
newstring[I] = table[oldstring[I]]; //just showing this because if/then/else/case/etc is all unnecessary apart from being told you must do it.
not sure what you are allowed.
If allowed I would make
char condition = l if its a lower letter, n if its a number, 0 if its unchanged, and so on. then
switch(condition)
case('l')
result = letter - 64;
break;
case('n')
whatever for number
…
default
do nothing
/*okay so this is my code, so far its working except one minor issue, i can successfully encode lowercase letters,space,period,and new line. also if a upper csse letter is read it prints it as is ( assignment says to do that). but the issue i have is when it reads something else for instance any of these characters !@#$%^&*()_+-={[}]|\:;"'<,>/? they are encoded as G when they should be skipped over and printed as is. i am not sure why only these characters are the only one to do so, each and every one gets mapped to G */
#include<stdio.h>
int main(){
char EorD;
printf("are you encoding (e) or decoding (d) a file?");
scanf("%c",&EorD);
while (EorD!= 'd' && EorD != 'e') {