![]() |
Sharky Extreme : Forums: |
|
![]() |
| ||||||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
Goldfish
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
|
assignment: 4.4. Internet mailers and news-readers often contain a "rot13"
function that can be used to "encrypt" words by rotating their characters 13 positions (i.e., A becomes N, B becomes O, C becomes P, ... Z becomes M). This is convenient, because the same function can be used to both "encrypt" and "decrypt" a word: rot13("ROMEO") == "EBZRB" and rot13("EBZRB") == "ROMEO" Write a Rot13() function that, given a string, returns a string whose characters are those of the first string rotated 13 positions. (Hint: You may find the % operator to be useful.) my program thus far and its error messages can be found at http://www.spaghettibasgetti.net/rot13error.txt If anyone can help me, I will be eternally greatful. Thank you ![]() ------------------ I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh?
__________________
I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh? |
|
|
|
|
|
#2 |
|
Hammerhead Shark
Join Date: Feb 2001
Posts: 1,612
|
void main()
{ cout << "Input the sentence you want translated:"; string rot13 (string normalWords); } Is that how you call a function in C++? I've barely used C++ at all, but if that were C, I'd wonder why you put the "string" in front of rot13. (btw, why aren't you using the % operator like it said? =) |
|
|
|
|
|
#3 |
|
Goldfish
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
|
Okay, I'm a dolt. I just figured out my mistake. hehe. thank you to anyone who tried. I realised that i needed to declare my variables inside the main function, rather than out in never never land... well, that took away the errors anyhow... anyone know why is was so wrong?
------------------ I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh?
__________________
I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh? |
|
|
|
|
|
#4 |
|
Goldfish
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
|
Oh, no, the errors came back as soon as i defined rot locally in the second function.
Oh, Strogian, yes, that is how you call the functions in C++... i use string because i want letters rather than numbers, although i'm beginning to think i should just use int... [integer] and i'm not using the % operator like it said because i can't think of how i could use it effectively. ![]() ------------------ I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh?
__________________
I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh? |
|
|
|
|
|
#5 |
|
Tiger Shark
Join Date: Mar 2001
Posts: 545
|
Time to once again over-help someone with an assignment
![]() Code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string normalWords, rot;
void main()
{
cout << "Input the sentence you want translated:";
string rot13 (string normalWords);
}
but here's what I think you wanted to do: Code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
// This is a prototype. You had yours inside the main program, which I'm not even
// sure is legal.
string rot13(string nomralWords);
void main()
{
// Declare the variable names as local variables.
// They were global before, meaning that they would
// be overwritten in your function. This is a bad
// thing
type "string," as I stay away from most C++ types ![]() If it doesn't work, I can help you use char[length] and char* ![]() Code:
string rot13 (string normalWords)
{
for (;
Of that does not make sense. Why pass it in as a parameter to the function if you're just going to read it in? I'm guessing the function assumes that normalWords already has a value. Also, you read in normalWords EVER iteration of the loop. But wait, the loop returns after one character. Also, "normalWords[i];" does not make sense at all. Maybe I'm wrong, but I don't think this will work. My version: Code:
string rot13(string someString)
{
// We need a variable to use in this function
string newString;
// Loop through the string.
// Not sure if strlen works on strings, so
// you'll have to fix it if it doesn't
for (int i = 0; i <= strlen(someString); i++)
{
// If the character is a valid letter, rotate it.
// Just copy any other character
if ((toupper(someString[i]) >= 'A') && (toupper(someString[i]) <= 'Z'))
{
// If the letter is between A and M,
// Add 13 to make it a letter between N and Z.
// Otherwise, do the inverse.
if (toupper(someString[i]) <= 13)
{
newString[i] = someString[i] + 13;
}
else
{
newString[i] = someString[i] - 13;
}
}
else
{
newString[i] = someString[i];
}
}
return newString;
}
I haven't tested this code, so I don't know if it will even compile. I'll leave testing and fixing anything up to you
__________________
System specs: CPU: Athlon 64 X2 4400+ Mobo: Asus A8N-SLI Premium RAM: 1024 MB Corsair ValueSelect DDR 3200 Video: BFG 7800 GTX OC Sound: SBLive! mp3+ (waiting for X-Fi) OS: Windows XP Home Edition |
|
|
|
|
|
#6 |
|
Goldfish
Join Date: May 2001
Location: Lost in my mind... Or is that Lost my mind?
Posts: 69
|
Thank you so much for your help.
Along with your help and others', I finally got it working Code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string rot13 (string normalWords);
void main()
{
string normalWords;
cout << "Input the sentence you want translated:";
cin >> normalWords;
cout << "\nThe rot-13 translation is: " << rot13(normalWords) << "\n\n\n";
}
string rot13 (string normalWords)
{
string rot = normalWords;
int i = 0;
for (i=0; i<normalWords.size(); i++)
{
rot[i]=normalWords[i];
normalWords[i];
if ((normalWords[i]>64) && (normalWords[i]< 91))
rot[i] =((normalWords[i]-65+13)%26)+65;
if ((normalWords[i]>96) && (normalWords[i]<123))
rot[i] =((normalWords[i]-97+13)%26)+97;
}
return rot;
}
THANK YOU for everything!!! I believe that I will probably pop in here again soon. hehe. I always enjoyed the coding forum ![]() ------------------ I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh?
__________________
I lost my toe the other day... then I looked down at my foot and found he'd come back to me. Guess I'll think twice about putting toes in my mouth,eh? |
|
|
|
|
|
#7 |
|
Reef Shark
Join Date: Oct 2000
Posts: 397
|
Should work nicely (caps only):
Code:
string rot13( string s )
{
static const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( int i = 0; i < s.length(); ++i ) {
s[i] = alphabet[(s[i]-'A'+13)%alphabet.length()];
}
return(s);
}
|
|
|
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|