Keep reading to view my question?
I have a text file that appears to be in some type of code.
Here are the rules of the code (this is how the file was created.)
If a character is not an Alphabetic character do nothing to it.
If it is an alpha character shift it 3 backwards in the alphabet.
Don't forget to wrap the alphabet (e.g.. 'b' would become 'y')
Don't forget to handle upper and lower case (especially when wrapping.)
I am trying to write a C program that will decode the file. To do this the file should reverse the process.
The input file is provided. The program should read the input file, decode it, and write the output file. Make sure that you check for proper opening of the file and print an error message if the file does not exist. You should open the output file for write ('w'). Don't forget to close all files.
Here are some hints to get you started.
You will still pass non-alpha characters
You will shift up 3 letters (not back)
You will still need to wrap the ends of the alphabet
Remember that the computer thinks 'W'=87 (and 'w'=119)
fgets and fputs are your friends
You can assume a maximum length of the input line of 80
Your function should take the 2 strings for an argument
With completion with this question I will need the following to go along with the answer.
Your source code (in a .c or a .cpp file)
Your solution file (a .txt file with the decoded message in it)
A couple of paragraphs describing your approach to solving the problem.
This is one approach that was used by someone else:
//begin
initialize your variables
open files that you will need
while not eof
read input file
call decode(instring, outstring)
write output file
close files
//end
function decode(instring, outstring)
initialize counter
looping through string
if alpha
if upper
process upper character
if lower
process lower character
else
pass non alpha charater
return from decode
Ql yb, lo klq ql yb-qexq fp qeb nrbpqflk:
Tebqebo 'qfp klyibo fk qeb jfka ql prccbo
Qeb pifkdp xka xooltp lc lrqoxdblrp cloqrkb
Lo ql qxhb xojp xdxfkpq x pbx lc qolryibp
Xka yv lmmlpfkd bka qebj. Ql afb, ql pibbm-
Kl jlob--xka yv x pibbm ql pxv tb bka
Qeb ebxoqxzeb, xka qeb qelrpxka kxqroxi pelzhp
Qexq cibpe fp ebfo ql. 'Qfp x zlkprjjxqflk
Abslrqiv ql yb tfpeba. Ql afb, ql pibbm-
Ql pibbm--mbozexkzb ql aobxj: xv, qebob'p qeb ory,
Clo fk qexq pibbm lc abxqe texq aobxjp jxv zljb
Tebk tb exsb percciba lcc qefp jloqxi zlfi,
Jrpq dfsb rp mxrpb. Qebob'p qeb obpmbzq
Qexq jxhbp zxixjfqv lc pl ilkd ifcb.
Clo tel tlria ybxo qeb tefmp xka pzlokp lc qfjb,
Qe' lmmobpplo'p tolkd, qeb molra jxk'p zlkqrjbiv
Qeb mxkdp lc abpmfpba ilsb, qeb ixt'p abixv,
Qeb fkplibkzb lc lccfzb, xka qeb pmrokp
Qexq mxqfbkq jbofq lc qe' rktloqev qxhbp,
Tebk eb efjpbic jfdeq efp nrfbqrp jxhb
Tfqe x yxob ylahfk? Tel tlria cxoabip ybxo,
Ql dorkq xka ptbxq rkabo x tbxov ifcb,
Yrq qexq qeb aobxa lc pljbqefkd xcqbo abxqe,
Qeb rkafpzlsboba zlrkqov, colj telpb ylrok
Kl qoxsbiibo obqrokp, mrwwibp qeb tfii,
Xka jxhbp rp oxqebo ybxo qelpb fiip tb exsb
Qexk civ ql lqebop qexq tb hklt klq lc?
Qerp zlkpzfbkzb albp jxhb zltxoap lc rp xii,
Xka qerp qeb kxqfsb erb lc obplirqflk
Fp pfzhifba l'bo tfqe qeb mxib zxpq lc qelrdeq,
Xka bkqbomofpb lc dobxq mfqze xka jljbkq
Tfqe qefp obdxoa qebfo zroobkqp qrok xtov
Xka ilpb qeb kxjb lc xzqflk. - Plcq vlr klt,
Qeb cxfo Lmebifx! - Kvjme, fk qev lofplkp
Yb xii jv pfkp objbjyboba.
This is what i have. it is suppose to take the input from the user and output Shakespeare's Hamlet but I am confused on what i am doing wrong. As fas as errors when i debug the problem it displays 0 errors and 1 warning and the 1 warning points to the bottom of the program where it displays char ch; and it says that char ch; is an unrefferenced variable.
I want to know what is wrong with my program and why is it not taking in the scrambled up text and automatically decoding the scrambled text and out putting William Shakespeare's Hamlet
#include <stdio.h>
void decode(char *inst, char *outst)
{
int i=0;
char ch;
while(inst[i]!='\0')
{
ch = inst[i];
if(ch>=65 && ch<=90)
{
ch = ch + 3;
if(ch>90)
{
ch = ch - 90;
ch = 64 + ch;
}
}
else if(ch>=97 && ch<=122)
{
ch = ch + 3;
if(ch>122)
{
ch = ch - 122;
ch = 96 + ch;
}
}
outst[i] = ch;
i++;
}
outst[i] = '\0';
}
int main()
{
FILE *in;
FILE *out;
char infile[20], outfile[20];
char instr[80],outstr[80];
char ch;
printf("Enter input file name: ");
scanf("%s",infile);
printf("Enter output file name: ");
scanf("%s",outfile);
in = fopen(infile,"r");
out = fopen(outfile,"w");
while(1)
{
fgets(instr,80,in);
if(feof(in))
break;
decode(instr,outstr);
fputs(outstr,out);
}
fclose(in);
fclose(out);
return 0;
}