c++ encoding problem

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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')  {
	
scanf("%c",&EorD);

}
int cc;
FILE* fp = fopen("in.txt","r");
FILE* fp2 = fopen("outprog3.txt","w");

while(fscanf(fp,"%c",&cc)==1)
{ 
if (cc>=97 && cc<=122)
	{
	char output;	
	output = cc - 64;
	fprintf(fp2,"%c",output);
	 }
	
else if (cc>=48 && cc<=57)
	{
	char output;	
	output = cc + 11;
	fprintf(fp2,"%c",output);	
	}
else if (cc=32)
{char output;
output = 69;
fprintf(fp2,"%c",output);
}
else if (cc=46)
{char output;
output = 70;
fprintf(fp2,"%c",output);
}
else if (cc=10)
{char output;
output = 71;
fprintf(fp2,"%c",output);
}
else
{fprintf(fp2,"%c",cc);}

}

}

	
Last edited on
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



Last edited on
/*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') {

scanf("%c",&EorD);

}
int cc,code;
FILE* fileinput = fopen("in.txt","r");
FILE* fileoutput = fopen("outprog3.txt","w");



while(fscanf(fileinput,"%c",&cc)==1)

{char temp;
temp = cc;



if (EorD == 100){
if (temp >= 33 && temp <= 58)
{ code = 1; }
else if (temp >=59 && temp <= 68)
{ code = 2; }
else if (temp == 69)
{ code = 3; }
else if (temp == 70)
{ code = 4; }
else if (temp == 71)
{ code = 5; }

switch(code)
{case 1:
temp = temp + 64;
fprintf(fileoutput,"%c",temp);
break;
case 2:
temp = temp - 11;
fprintf(fileoutput,"%c",temp);
break;
case 3:
temp = 32;
fprintf(fileoutput,"%c",temp);
break;
case 4:
temp = 46;
fprintf(fileoutput,"%c",temp);
break;
case 5:
temp = 10;
fprintf(fileoutput,"%c",temp);
break;
default :
temp = temp;
fprintf(fileoutput,"%c",temp);
}

}


else if (EorD == 101){

if (temp>=97 && temp<=122) //lower case
{ code = 1; }
else if (temp>=48 && temp<=57) //0-9
{ code = 2; }
else if (temp == 32) //space
{ code = 3; }
else if (temp == 46) //period
{ code = 4; }
else if (temp == 10)//new line
{ code = 5; }
switch(code)
{case 1:
temp = temp - 64;
fprintf(fileoutput,"%c",temp);
break;
case 2:
temp = temp + 11;
fprintf(fileoutput,"%c",temp);
break;
case 3:
temp = 69;
fprintf(fileoutput,"%c",temp);
break;
case 4:
temp = 70;
fprintf(fileoutput,"%c",temp);
break;
case 5:
temp = 71;
fprintf(fileoutput,"%c",temp);
break;
default :
temp = temp;
fprintf(fileoutput,"%c",temp);
break;

}
}


}
fclose(fileinput);
fclose(fileoutput);
return(0);

}
Topic archived. No new replies allowed.