473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Rot-13 encoding an std::string

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
Jan 28 '06 #1
9 3631
"Mikael S. H." <mi****@alminde.org> wrote in message
news:43**********************@nntp02.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.
Jan 28 '06 #2
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:4n**************@fe02.lga...
"Mikael S. H." <mi****@alminde.org> wrote in message
news:43**********************@nntp02.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.
Jan 29 '06 #3
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;
}
};

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

Jan 29 '06 #4

Peteris Krumins wrote:
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 ;)
P.Krumins

Jan 29 '06 #5

"Peteris Krumins" <pe*************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Jan 29 '06 #6
yeap a bug.
had different code before, forgot about - n before posted.
P.Krumins

Jan 29 '06 #7
On Sun, 29 Jan 2006 00:08:10 +0100, "Mikael S. H."
<mi****@alminde.org> wrote in comp.lang.c++:
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/~a...FAQ-acllc.html
Jan 30 '06 #8
> 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
Jan 30 '06 #9

Jack Klein wrote:
On Sun, 29 Jan 2006 00:08:10 +0100, "Mikael S. H."
<mi****@alminde.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.
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

Jan 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter M Aarestad | last post by:
I tried posting this at PerlMonks.org, but didn't get very far... So I wrote a Perl script about 5 months ago to parse a rather strange text file. Here's a sample of the source file: AACE_1:...
15
by: Francois De Serres | last post by:
hiho, what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to the string 'spam'? TIA, Francois
12
by: Rob Morris | last post by:
Hi, I'm teaching myself C for fun. I wrote the litle program listed below to convert rot13 text. It reads one char at a time and converts it via pointers. The constant char* letters holds the...
14
by: shappen | last post by:
Is there a function to do this ? with the string parameter,it return the encryption result of the string ?
0
by: Johan | last post by:
Hi I have a small problem I doesn't seem to manage very well. The thing is that I would like to retrive two instances of a program (two instances of the program is running)that are registered in...
2
by: Cody Manix | last post by:
is there a way to explicitly assign a name to an enum member? what about internationalisation? one must be able to change the text. i'd like to implemtent it this way: enum Color = {...
0
by: Wes | last post by:
Can anyone tell me how to change this C# code to grab all instances of an application that is running? Right now, it only will output to the first opened single document interface application n...
12
by: DumberThanSnot | last post by:
is there a faster way to copy an ArrayList of strings to a string other than a tight loop. In it's most simple terms, I'm currently using something like this... ---------------------------- ...
9
by: cyberscout | last post by:
OK I have some code which I didn't write and I'm toying with whether I need to tidy it up. In the code is the line shown in Example 1 Exampe 1: sprintf(stringvariable, "%s", "String"); ...
92
by: =?Utf-8?B?bW9iaWxlbW9iaWxl?= | last post by:
I'm trying to load this structure for a call to DeviceIoControl: typedef struct _NDISUIO_QUERY_OID { NDIS_OID Oid; PTCHAR ptcDeviceName; UCHAR Data; } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID; ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.