Thread: ROT13 Cypher

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    17

    ROT13 Cypher

    I read about ROT13 cyper and tried to make one myself.

    Code:
    void rot13(char *start){
        int i;
        for (i=0; i<strlen(start); i++)
        {
            if( (*start>='a' && *start<'n') || (*start>='A' && *start<'N') )
                *start += 13;
            else
                *start -= 13;
        }
    }
    It didn't work for me and then found a code online,
    Code:
    void rot13(char *text){
        for(;*text;text++)
        {
            if(*text>'A'&&*text<'N'||*text>'a'&&*text<'n')*text+=13;
            else if(*text>'M'&&*text<'Z'||*text>'m'&&*text<'z')*text-=13;
        }
    }
    (I might've edited my code after reading the online code )

    But why is mine not working?
    And, in second code, A,z,Z,z doesn't satisfy any condition, or am I not understanding the code correctly?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What's not working about it? It would help immensely if you provided useful details, like what input you gave the rot13 function, what output you expect, and what output you actually got.

    As it happens, you never change start, so *start always points to the same character, thus, you only rotate that one character. Note, your loop and the other loop are very different. Either increment start like they do (and fix your loop to check *start for termination), or use start[i]. Also, you should avoid putting strlen in the actual loop, since it will be called every time. Instead, use a temporary variable:
    Code:
    int len = strlen(start);
    for (i = 0; i < len; i++)

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Sourabh Verma View Post
    But why is mine not working?
    And, in second code, A,z,Z,z doesn't satisfy any condition, or am I not understanding the code correctly?
    In your version you are using the variable `i' perhaps so that you can use it for indexing your array. However you never use the index when you access it. Generally you choose one or the other

    Array indexing
    Code:
    for (int i=0; i < n; i++)
       foo[i] = bar;
    Pointer dereferencing
    Code:
    for (char *s = foo; *s; s++)
       *s = bar;
    Also in your version, what happens when the characters are not alphabetic? In ROT13 you should only be changing the alphabetic characters, hence you need the second else if to check if it's at the last half of the alphabet.

    Finally in both versions, what happens when an alphabet is 'a' or 'z' and what should happen? For example 'A' > 'A' is false. I think you should use <= >=, etc. to define inclusive boundaries for this.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    17

    Fixed!!

    Thanks everyone!
    I got it working. Here's my final code:

    Code:
    void rot13_1(char *start){
        int i, len = strlen(start);
        for (i=0; i<len; i++)
        {
            if( (*(start+i)>='a' && *(start+i)<'n') || (*(start+i)>='A' && *(start+i)<'N') )
                *(start + i) += 13;
            else if ( (*(start+i)>'m' && *(start+i)<'z') || (*(start+i)>'M' && *(start+i)<'Z') )
                *(start +i) -= 13;
        }
    }
    This code will fail if there is no '\0' character at end, right?
    Any way to solve this problem?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Sourabh Verma View Post
    This code will fail if there is no '\0' character at end, right?
    Any way to solve this problem?
    Yes, it will fail, and no, there is no way to solve it from within the function. The function works with strings. If there is no '\0', then it's not a string, so the person calling it is passing bad data. Garbage in, garbage out.

    As a side note, it would be much more legible if you did:
    Code:
    start[i]
    // instead of
    *(start+i)
    Last edited by anduril462; 02-14-2013 at 10:34 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You are still ignoring 'z' and 'Z'.

    Aren't you testing your code? You should always test the edge cases/boundaries (like here all the characters in the if statements).

    Bye, Andreas
    Last edited by AndiPersti; 02-14-2013 at 10:59 AM.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Quote Originally Posted by Sourabh Verma View Post
    Any way to solve this problem?
    You could treat it as a binary string instead of a c-style string, but you *cannot* use c-style string functions on the char array anymore (unless you append '\0').

    Keep track of the length and pass it with the "binary string" to your function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. caesar cypher
    By me77 in forum C Programming
    Replies: 17
    Last Post: 04-03-2009, 07:48 AM
  2. Caesar Cypher
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-29-2007, 12:21 PM
  3. Error in Rot13 function
    By lala123 in forum C Programming
    Replies: 15
    Last Post: 07-04-2005, 05:46 AM
  4. rot13 decoding/encoding help!!!!!
    By zaff1 in forum C++ Programming
    Replies: 0
    Last Post: 02-06-2003, 04:59 PM

Tags for this Thread