Rot-13 encoding an std::string

M

Mikael S. H.

I there a function in the string class to replace an 'a' with an 'm', or
something similar?

Will I have to write a function iterating over every character in the
string to rot-13 encoding a string?
 
J

Jim Langston

Mikael S. H. said:
I there a function in the string class to replace an 'a' with an 'm', or
something similar?

Will I have to write a function iterating over every character in the
string to rot-13 encoding a string?

std::string MyString = "This is a test. 123. $%*"
for ( int i = 0; i < MyString.length(); ++i )
MyString = MyString + 13;

This should do it, and I don't beleive you even have to worry about
overflow.

I think you could use the for_each also, but I don't use those so don't know
the correct syntax.

Of course, the only thing I know about rot-13 is that it adds 13 to the
characters. I think it does special cases for unprintable characters (0 to
25?) and other ones so it can be sent as pure text. You'll need to get the
full details of rot-13 to find the specifics.
 
J

Jim Langston

Jim Langston said:
Mikael S. H. said:
I there a function in the string class to replace an 'a' with an 'm', or
something similar?

Will I have to write a function iterating over every character in the
string to rot-13 encoding a string?

std::string MyString = "This is a test. 123. $%*"
for ( int i = 0; i < MyString.length(); ++i )
MyString = MyString + 13;

This should do it, and I don't beleive you even have to worry about
overflow.

I think you could use the for_each also, but I don't use those so don't
know the correct syntax.

Of course, the only thing I know about rot-13 is that it adds 13 to the
characters. I think it does special cases for unprintable characters (0
to 25?) and other ones so it can be sent as pure text. You'll need to get
the full details of rot-13 to find the specifics.


I actually went and spent the 30 seconds after I typed this to see what
rot-13 actually did. It seems it only applies to 'a' thorugh 'z' and 'A'
throught 'Z'. Of course it even had source code.

http://b-con.us/code/rot-13_c.php

A simple STFW found this.
 
P

Peteris Krumins

Mikael said:
I there a function in the string class to replace an 'a' with an 'm', or
something similar?

Will I have to write a function iterating over every character in the
string to rot-13 encoding a string?

Here is an example code that creates new data type 'rot_string' which
upon instantiation
rot13'es a string.


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

struct rot_char_traits : public std::char_traits<char>
{
static char *copy(char *s1, const char *s2, size_t n) {
while (n--) *s1++ = isalpha(*s2 + 13) ? *s2++ + 13 : *s2++ -
13;
return s1 - n;
}
};

typedef std::basic_string<char, rot_char_traits> rot_string;

int
main()
{
rot_string nun = "aha"; // "aha" rot13'ed is "nun"
rot_string sync = "flap"; // "flap" rot13'ed is "sync"

std::cout << nun.c_str() << "\n";
std::cout << sync.c_str() << "\n";

return 0;
}


P.Krumins
 
P

Peteris Krumins

Peteris said:
Here is an example code that creates new data type 'rot_string' which
upon instantiation
rot13'es a string.


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

struct rot_char_traits : public std::char_traits<char>
{
static char *copy(char *s1, const char *s2, size_t n) {
while (n--)
*s1++ = isalpha(*s2 + 13) ? *s2++ + 13 : *s2++ - 13;
return s1 - n;
^ bug ;)


P.Krumins
 
J

Jim Langston

Peteris Krumins said:
Here is an example code that creates new data type 'rot_string' which
upon instantiation
rot13'es a string.


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

struct rot_char_traits : public std::char_traits<char>
{
static char *copy(char *s1, const char *s2, size_t n) {
while (n--) *s1++ = isalpha(*s2 + 13) ? *s2++ + 13 : *s2++ -
13;
return s1 - n;

n at this point should be 0 since while (n--). So this will simply return
s1 which will be pointing off the end of the string. Suggest copy s1 in
beginning to return here or copy n to use in return here, or copy n to
another var and decrement that instead.
 
P

Peteris Krumins

yeap a bug.
had different code before, forgot about - n before posted.


P.Krumins
 
J

Jack Klein

I there a function in the string class to replace an 'a' with an 'm', or
something similar?

Will I have to write a function iterating over every character in the
string to rot-13 encoding a string?

Remember that rot-13 would most likely do truly awful things on an
implementation that used something other than the ASCII character set.
 
S

Stephen Howe

struct rot_char_traits : public std::char_traits said:
{
static char *copy(char *s1, const char *s2, size_t n) {
while (n--) *s1++ = isalpha(*s2 + 13) ? *s2++ + 13 : *s2++ -
13;
return s1 - n;
}
};

Consider what happens if s2 is pointing to a string which has "abc123xyz"
What happens to the non-alphabetical characters?
Is what happens consistent with ROT13 encoding?

Stephen Howe
 
M

Michiel.Salters

No. Just write a function that rot-13 encodes one character, and use it
with
std::transform.
Remember that rot-13 would most likely do truly awful things on an
implementation that used something other than the ASCII character set.

No. Dumb implementations may, though.

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top