C++ ROT13

By wade2462 on May 26, 2010

A ROT13 function that uses a switch. Simply pass in a string and get the modified string back. wait.h is simply a header I wrote to suspend the program using cin.get();

#include <string>
#include <iostream>
#include "wait.h"

string ROT13(string original)
{
    string result = original;
    for (unsigned int a = 0; a<result.length(); ++a)
    {
        switch (result[a])
        {
            case 'a':
                result[a] = 'n';
                break;
            case 'A':
                result[a] = 'N';
                break;
            case 'b':
                result[a] = 'o';
                break;
            case 'B':
                result[a] = 'O';
                break;
            case 'c':
                result[a] = 'p';
                break;
            case 'C':
                result[a] = 'P';
                break;
            case 'd':
                result[a] = 'q';
                break;
            case 'D':
                result[a] = 'Q';
                break;
            case 'e':
                result[a] = 'r';
                break;
            case 'E':
                result[a] = 'R';
                break;
            case 'f':
                result[a] = 's';
                break;
            case 'F':
                result[a] = 'S';
                break;
            case 'g':
                result[a] = 't';
                break;
            case 'G':
                result[a] = 'T';
                break;
            case 'h':
                result[a] = 'u';
                break;
            case 'H':
                result[a] = 'U';
                break;
            case 'i':
                result[a] = 'v';
                break;
            case 'I':
                result[a] = 'V';
                break;
            case 'j':
                result[a] = 'w';
                break;
            case 'J':
                result[a] = 'W';
                break;
            case 'k':
                result[a] = 'x';
                break;
            case 'K':
                result[a] = 'X';
                break;
            case 'l':
                result[a] = 'y';
                break;
            case 'L':
                result[a] = 'Y';
                break;
            case 'm':
                result[a] = 'z';
                break;
            case 'M':
                result[a] = 'Z';
                break;
            case 'n':
                result[a] = 'a';
                break;
            case 'N':
                result[a] = 'A';
                break;
            case 'o':
                result[a] = 'b';
                break;
            case 'O':
                result[a] = 'B';
                break;
            case 'p':
                result[a] = 'c';
                break;
            case 'P':
                result[a] = 'C';
                break;
            case 'q':
                result[a] = 'd';
                break;
            case 'Q':
                result[a] = 'D';
                break;
            case 'r':
                result[a] = 'e';
                break;
            case 'R':
                result[a] = 'E';
                break;
            case 's':
                result[a] = 'f';
                break;
            case 'S':
                result[a] = 'F';
                break;
            case 't':
                result[a] = 'g';
                break;
            case 'T':
                result[a] = 'G';
                break;
            case 'u':
                result[a] = 'h';
                break;
            case 'U':
                result[a] = 'H';
                break;
            case 'v':
                result[a] = 'i';
                break;
            case 'V':
                result[a] = 'I';
                break;
            case 'w':
                result[a] = 'j';
                break;
            case 'W':
                result[a] = 'J';
                break;
            case 'x':
                result[a] = 'k';
                break;
            case 'X':
                result[a] = 'K';
                break;
            case 'y':
                result[a] = 'l';
                break;
            case 'Y':
                result[a] = 'L';
                break;
            case 'z':
                result[a] = 'm';
                break;
            case 'Z':
                result[a] = 'M';
                break;
        }
    }
    return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
    string a;
    getline(cin, a);
    cout << ROT13(a) << endl;
    wait();
    return 0;
}

Comments

Sign in to comment.
sunslayer   -  May 27, 2010

instead of using a switch statement you can use the ASCII value of the character

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;
      }
}
 Respond  
raccoon   -  May 26, 2010

wow, thanks for the excellent example of using case statements.

Do you happen to know if C compilers optimize switch/case so that this would run as fast as a look-up table array?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.