I need help with ROT-13 encoding and decoding

I do have little bit of program written out, but I am not getting the result I want. If someone could help me out, and explain it in a good bit of detail it would be a huge huge help.

Here is what I have to do:
Write a program to the following specifications to encode and decode text using the "Rot-13" encoding.

Input some text terminated by hitting Enter twice, then output the encoded and decoded text. Repeat as the user desires.

The code is ROT-13, where, if the character is an uppercase alphabetic character, you add 13 to the difference between the character and 'A', divide by 26, take the remainder and add it to 'A'. If the character is lowercase, do the same thing using 'a' in place of 'A'. If it is not alphabetic, don't change it.
Examples:
'A' is 65 and 'a' is 97, so
('B'-'A') is 1, (1+13) is 14, (14 % 26) is 14 and ('A'+14) is 79, which is 'O'.
('a'-'a') is 0, (0+13) is 13, (13 % 26) is 13 and ('a'+13) is 110, which is 'n'.
('y'-'a') is 24, (24 + 13) is 37, (37 % 26) is 11 and ('a'+11) is 108, which is 'l'.
So "Bay" should be encoded as "Onl". Note that "Onl" would be encoded as "Bay".

Sample run (user input in bold):

Enter text, and press enter twice at end:
This is a sample of some text to be encoded with ROT-13.

Guvf vf n fnzcyr bs fbzr grkg gb or rapbqrq jvgu EBG-13.
This is a sample of some text to be encoded with ROT-13.

Again (y/n)? y
Enter text, and press enter twice at end: Bay

Onl
Bay
Again (y/n)? n

You must define functions in your program. A suggested set of functions includes the following, although other additional methods may be useful:
A function to prompt for and input the text and return it as a String.
A function to encode one character and return the encoded char
A function to encode an entire String, returning the encoded String

Here is what I have so far:

#include <iostream> //input output and file input output
#include <fstream>

using namespace std;
int main () {

char msg[260]; // intializes the variable sentence array, sentence refers to first 20 characters

cout << "Please enter your sentence" << endl;
cin.getline(msg,26);//gets the sentence

for (int i=0; i<26; i++) //encodes the sentence into ROT13
{
if (msg[i] >= 'A' && msg[i] <= 'Z'){
msg[i]+=13;
if (msg [i] > 'Z'){
msg[i] -=26;
}//End of if
}//End of if

if (msg[i] >= 'a' && msg[i] <= 'z'){
msg[i] +=13;
if (msg[i] > 'z'){
msg[i] -=26;
}//End of if
}//End of if

}//End of for
for (int i=0; i<26; i++) //prints encoded sentence
{
cout << msg[i];
}//End of for

return 0;
}//End of main

....
I know I am not completely done with it..but I really need help with this.
Hi trex123,

Please post your code in code tags - the <> button on the right under format.

So what is the problem exactly - what results were you expecting?
How many characters are you supposed to be working on in your sentence?

You array is 260, yet indicates that you only want 20 of those.

Why have such a long array?

Furthermore, when it comes to encoding, you're operating on 26 characters, not 20 or 260. It just needs to be a little more consistent.

Maybe declare a constant for the max sentence length and use that throughout.

As a general preference, I would also opt for the string class over the char[].
well I changed my code here is the new one. It works fine but I can not figure out how to have the user hit enter twice in order for the program to run, and ask if they want to do it again..if you can help me with it, it would be great. thank you.
here is the New Code:


#include <cctype>
#include <iostream>
#include <string>

/**
* * \brief Apply the ROT13 algorithm to a string.
* * \param source str text to apply the algorithm to.
* * \return The encoded text is returned.
* */
using namespace std;

string ROT13(string str)
{
string encoded;
for (size_t i = 0; i < str.size(); ++i) {
if (isalpha(str[i])) {
if ((tolower(str[i]) - 'a') < 14)
encoded.append(1, str[i] + 13);
else
encoded.append(1, str[i] - 13);
} else {
encoded.append(1, str[i]);
}
}
return encoded;
}

int main()
{
string str;
string again;
cout << "Enter the text and hit enter twice: " << flush;
getline(cin, str);
cout << "Encoded Text : " << ROT13(str) << endl;

}
Take a while loop and ask user, if he/she wants to stop the program.
Hint:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	bool quit = false;

	while(!quit) {
		string str;
		string again;
		cout << "Enter the text and hit enter twice: " << flush;
		getline(cin, str);
		cout << "Encoded Text : " << ROT13(str) << endl;
	}
}
Last edited on
Shadow123,
thank you, it works but it just keeps going. how can i ask the user if they want to continue. i can ask them if they want to continue..but my main problem is assigning them Y/N for it...can i get a hint for that. and also i tried a lot, but i can get the user to press enter twice in order to run the program.
thank you for you help. it was much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	bool quit = false;
	string yn; // string for user input

	while(not quit) {
		string str;
		string again;
		cout << "Enter the text and hit enter twice: " << flush;
		getline(cin, str);
		cout << "Encoded Text : " << ROT13(str) << endl;
		cout << "Another try? (Y/N) "; // ask user, if he/she wants again
		cin >> yn; // taking user input
		cin.ignore(1024, '\n'); // ignore rest of input buffer (clearing input memory)
		if(yn != "Y" && yn != "y") // well, test user input. other input as Y/y will end the program
			quit = true;
	}
}

Well, but you should be able by yourself to prompt the user to hit return twice to close the program...
Last edited on
worked perfectly...all i needed was a if statement...i don't know why didn't it hit me before.
Thank you very much for your help!
Topic archived. No new replies allowed.