CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Dec 2006
    Posts
    12

    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

  2. #2
    Join Date
    Sep 2006
    Location
    Sunshine State
    Posts
    517

    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

  3. #3
    Join Date
    Dec 2006
    Posts
    12

    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

  4. #4
    Join Date
    Sep 2006
    Location
    Sunshine State
    Posts
    517

    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

  5. #5
    Join Date
    Dec 2006
    Posts
    12

    Re: How to change letters of string?

    Ok, but what does pszString mean?

  6. #6
    Join Date
    Jan 2003
    Posts
    615

    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
    Code:
    Hello
    Ifmmp
    Last edited by laasunde; December 10th, 2006 at 07:20 AM.

  7. #7
    Join Date
    Dec 2006
    Posts
    12

    Re: How to change letters of string?

    So adding a number to a letter will shift the letter by the numbers amount?

  8. #8
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: How to change letters of string?

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

  9. #9
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  10. #10
    Join Date
    Dec 2006
    Posts
    12

    Re: How to change letters of string?

    When I try to compile that C++ program I get: http://rafb.net/paste/results/fHSKTq95.html.

  11. #11
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  13. #13
    Join Date
    Dec 2006
    Posts
    12

    Re: How to change letters of string?

    You were right I forgot to add "#include <string>.

    Thanks,
    Nathan

  14. #14
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  15. #15
    Join Date
    Dec 2006
    Posts
    12

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured