Programming and Problem Solving with C Sixth edition Brief P
Programming and Problem Solving with C++ Sixth edition (Brief): Programming Problem 2 Chapter 8 page 400. In addition to the problem, my professor asked us to translate this statement into ROT13 language: \"This is the day the Lord has made. Let us rejoice and be glad in it.\"
This is my code and in the output.rot13 file, the letters are translated, the full stop stays the same. However, there is no space in the output file. Please check the code and point out the problem.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Global declarations: Constants and type definitions only -- no variables
//Function prototypes
void Rot13(char inputChar, char &rot13);
void WriteTranslatedChar(char &ch, ofstream &inData);
int main()
{
//Variable declarations
string fileName;
char inputChar, rot13;
ifstream input;
ofstream output;
//Program logic
cout << \"Please input the name of the file: \";
cin >> fileName;
input.open(fileName);
if (!input)
{
cout << \"The file does not exist. Exiting the program.\";
system(\"pause\");
return(0);
}
output.open(\"output.ROT13\");
while (input >> inputChar)
{
Rot13(inputChar, rot13);
WriteTranslatedChar(rot13, output);
}
input.close();
output.close();
//Closing program statements
system(\"pause\");
return 0;
}
//Function definitions
void Rot13(char inputChar, char&rot13)
{
if (inputChar >= \'a\' && inputChar <= \'m\') rot13 = inputChar + 13;
else if (inputChar >= \'n\' && inputChar <= \'z\') rot13 = inputChar - 13;
else if (inputChar >= \'A\' && inputChar <= \'M\') rot13 = inputChar + 13;
else if (inputChar >= \'N\' && inputChar <= \'Z\') rot13 = inputChar - 13;
else rot13 = inputChar;
}
void WriteTranslatedChar(char &rot13, ofstream &output)
{
output << rot13;
}
Solution
ROT13:
ROT13 is a cipher and this cipher obscures the characters in a string.
With ROT13, characters are rotated 13 places in the alphabet.Other characters are not changed.
A methos in strings package can be used to translate strings.It receives a func that receives a rune,and returns a translated one.
For ROT13 methods, a translation method is usually the clearest approach.
A new string could be built up, in a loop over the characters, but this is less clear.
ROT13 is not a secure cipher. It can be easily be reversed.
but it might help reduce casual snooping on a piece of text.
And helps to know about the string transformation methods.
The problem is :
str is uninitialised and it is being dereffered in rot13,causing a crash.Allocate memory for str before passing to rot13()
char stris large enough to hold string and initialised.
You never allocate memory to for your output.
loop condition always evaluates to true (=asssigns and returns the assigned value ,== tests for equality).
increases or decreses the values in the return string by rot13.the values you placed in the output string +/13 from the inintial values in output string.it should be based on input string.
this doesn\'t handle \"A\",\'n\', or \"N\"..
This function can encode/decode or to/from of rot13 string.It\'s comptable.

