|
-
December 9th, 2006, 02:47 PM
#1
How to change letters of string?
Hi, I'm working my way through a book called "Information Security: Principles and Practice". At the end of each chapter there are problems that the author deals out to make sure you comprehend what you just read. Well, a lot of the problems are encrypted strings using the ceasar cipher, and I'm trying to shift the letters in a string to decrypt the string. How can I do this in C++?
Thanks for the help,
Nathan
-
December 9th, 2006, 03:30 PM
#2
Re: How to change letters of string?
What kind of string are you using?
char *, std::string or CString?
In case it is a char *, you can do the following:
Code:
char *pCursor = pszString;
while('\0' != *pCursor)
{
*pCursor = *pCursor + whatever; // Here you can modify every single char
pCursor++; // advance to next char
}
In case of std::string or CString, there are functions for replacing strings.
Or, you could get a ptr to the bytes and then use what I wrote above.
Hope that helps,
Andy
-
December 9th, 2006, 03:39 PM
#3
Re: How to change letters of string?
I haven't written the program yet, cause I wasn't sure how In your code you posted does it actually change a letter? I don't understand it very well, sorry. I'm kind of new to C++ so thanks for the help
-
December 9th, 2006, 03:59 PM
#4
Re: How to change letters of string?
No, but in the loop you can add/subtract the number you want (I understand that is what Caesarian cipher is, adding 3 to 'A', so it becomes 'D'.
E.g.
'A' + 3 = 'D' and
'D' - 3 = 'A'
If you don't understand why, look at an ASCII table.
-Andy
-
December 9th, 2006, 04:22 PM
#5
Re: How to change letters of string?
Ok, but what does pszString mean?
-
December 10th, 2006, 07:16 AM
#6
Re: How to change letters of string?
How about;
Code:
int main()
{
std::string s = "Hello";
std::cout << s << std::endl;
for(int index=0; index < s.length(); index++)
{
s[ index ] = s[ index ] + 1;
}
std::cout << s << std::endl;
system("PAUSE");
return 0;
}
Output
Last edited by laasunde; December 10th, 2006 at 07:20 AM.
-
December 10th, 2006, 11:42 AM
#7
Re: How to change letters of string?
So adding a number to a letter will shift the letter by the numbers amount?
-
December 10th, 2006, 11:48 AM
#8
Re: How to change letters of string?
 Originally Posted by Nathand
So adding a number to a letter will shift the letter by the numbers amount?
as suggested earlier look at the asciitable.
search asciitable on google...
-
December 10th, 2006, 12:00 PM
#9
Re: How to change letters of string?
@namezero111111: Warning, your encoded string may contain nul character.
Or, assuming that the source character set contains only A-Za-z and space, the destination character set doesn't match the source character set.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
December 12th, 2006, 09:29 AM
#10
Re: How to change letters of string?
When I try to compile that C++ program I get: http://rafb.net/paste/results/fHSKTq95.html.
-
December 12th, 2006, 09:38 AM
#11
Re: How to change letters of string?
@Nathand: Check that you include the <string> and <iostream> headers (both of them must be included).
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
December 12th, 2006, 09:38 AM
#12
Re: How to change letters of string?
A proper cipher (assuming only capital letters in the source)
Code:
char *in = source;
char *out = *dest; // assuming dest is pre-allocated
while (*in)
{
*out = ((*in -'A') + offset) %26;
in++; // not optimal, but possibly easier to read for a beginner...
out++;
}
*out = 0; // terminate the output.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 12th, 2006, 09:57 AM
#13
Re: How to change letters of string?
You were right I forgot to add "#include <string>.
Thanks,
Nathan
-
December 12th, 2006, 10:03 AM
#14
Re: How to change letters of string?
TheCPUWizard: Wouldn't it be:
Code:
*out = 'A' + ((*in -'A') + offset) %26;

And if space characters have to be handled:
Code:
if (*in == ' ') *out=*in;
else *out = 'A' + ((*in -'A') + offset) %26;
Yes, I know, space characters are not encrypted, it's very weak, but AFAIK, the original caesar encryption algorithm does that.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
December 12th, 2006, 10:20 AM
#15
Re: How to change letters of string?
Ok, last question 
When I use this code:
Code:
#include <iostream>
#include <string>
int key = 1;
int main()
{
std::string s = "CSYEVIXIVQMREXIH";
std::cout << s << std::endl;
for(int i = 0; i < 26; i++)
{
for(int index=0; index < s.length(); index++)
{
s[ index ] = s[ index ] + key;
}
std::cout << s << std::endl;
key += 1;
}
system("PAUSE");
return 0;
}
The caesar's cipher is "youareterminated", but the program returns "_ouareterminated". Why would this be? This also happens with some other ciphers I tried.
And if someone feels so inclined to compile and run this on their system, let me know if you get a very strange "beeping" noise coming from your computer?
Thanks,
Nathan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|