C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Rot-13 encoding an std::string

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mikael S. H.
Guest





PostPosted: Sun Jan 29, 2006 12:00 am    Post subject: Rot-13 encoding an std::string Reply with quote



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?

--
MikaelSh
Back to top
Jim Langston
Guest





PostPosted: Sun Jan 29, 2006 12:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote



"Mikael S. H." <mikael (AT) alminde (DOT) org> wrote in message
news:43dbf94d$0$4820$ba624c82 (AT) nntp02 (DOT) dk.telia.net...
Quote:
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?

--
MikaelSh

std::string MyString = "This is a test. 123. $%*"
for ( int i = 0; i < MyString.length(); ++i )
MyString[i] = MyString[i] + 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.
Back to top
Jim Langston
Guest





PostPosted: Sun Jan 29, 2006 12:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote



"Jim Langston" <tazmaster (AT) rocketmail (DOT) com> wrote in message
news:4nTCf.136$Zl7.29 (AT) fe02 (DOT) lga...
Quote:
"Mikael S. H." <mikael (AT) alminde (DOT) org> wrote in message
news:43dbf94d$0$4820$ba624c82 (AT) nntp02 (DOT) dk.telia.net...
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?

--
MikaelSh

std::string MyString = "This is a test. 123. $%*"
for ( int i = 0; i < MyString.length(); ++i )
MyString[i] = MyString[i] + 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.
Back to top
Jim Langston
Guest





PostPosted: Sun Jan 29, 2006 2:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote

"Peteris Krumins" <peteris.krumins (AT) gmail (DOT) com> wrote in message
news:1138497793.883097.264410 (AT) g49g2000cwa (DOT) googlegroups.com...
Quote:
Mikael S. H. wrote:
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;

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.
Back to top
Peteris Krumins
Guest





PostPosted: Sun Jan 29, 2006 2:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote

Peteris Krumins wrote:
Quote:
Mikael S. H. wrote:
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;
^ bug Wink



P.Krumins
Back to top
Peteris Krumins
Guest





PostPosted: Sun Jan 29, 2006 2:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote

Mikael S. H. wrote:
Quote:
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
Back to top
Peteris Krumins
Guest





PostPosted: Sun Jan 29, 2006 3:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote

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


P.Krumins
Back to top
Jack Klein
Guest





PostPosted: Mon Jan 30, 2006 12:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote

On Sun, 29 Jan 2006 00:08:10 +0100, "Mikael S. H."
<mikael (AT) alminde (DOT) org> wrote in comp.lang.c++:

Quote:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Back to top
Stephen Howe
Guest





PostPosted: Mon Jan 30, 2006 6:00 am    Post subject: Re: Rot-13 encoding an std::string Reply with quote

Quote:
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;
}
};

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
Back to top
Guest






PostPosted: Mon Jan 30, 2006 3:00 pm    Post subject: Re: Rot-13 encoding an std::string Reply with quote

Jack Klein wrote:
Quote:
On Sun, 29 Jan 2006 00:08:10 +0100, "Mikael S. H."
mikael (AT) alminde (DOT) org> wrote in comp.lang.c++:

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

No. Just write a function that rot-13 encodes one character, and use it
with
std::transform.

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.