简体   繁体   中英

Caesar Cipher in C++: How to convert a message to integers without too many if statements?

My first project for a comp sci class involves making a caesar cipher program. I started by converting the key(a letter AZ) to an int(0-25). I need to do this for the c-style arrays(strings) which contain the messages. I think the method I started doing would create an enormous amount of if else statements. Is there a quicker way to do this?

#include <iostream>
#include <proj1.h>
using namesapce std;

//Deciphers a message. cip[] is a char array containing a Cipher message                                                                                                                                    
//as a null-term.                                                                                                                                                                                           
void Decipher(char Cip[], char key);
{
  char intCip[]
  int intKey = 0;
  if(key == A)
    {
      intKey = 0;
    }
  else if(key == B)
    {
      intKey = 1;
    }
  else if(key == C)
    {
      intKey = 2;
    }
  else if(key == D)
    {
      intKey = 3;
    }
  else if(key == E)
    {
      intKey = 4;
    }
  else if(key == F)
    {
      intKey = 5;
    }
  else if(key == G)
    {
      intKey = 6;
    }
  else if(key == H)
    {
      intKey = 7;
    }
  else if(key == I)
    {
      intKey = 8;
    }
  else if(key == J)
    {
      intKey = 9;
    }
  else if(key == K)
    {
      intKey = 10;
    }
  else if(key == L)
    {
      intKey = 11;
    }
  else if(key == M)
    {
      intKey = 12;
    }
  else if(key == N)
    {
      intKey = 13;
    }
  else if(key == O)
    {
      intKey = 14;
    }
  else if(key == P)
    {
      intKey = 15;
    }
  else if(key == Q)
    {
      intKey = 16;
    }
  else if(key == R)
    {
      intKey = 17;
    }
  else if(key == S)
    {
      intKey = 18;
    }
  else if(key == T)
    {
      intKey = 19;
    }
  else if(key == U)
    {
      intKey = 20;
    }
  else if(key == V)
    {
      intKey = 21;
    }
  else if(key == W)
    {
      intKey = 22;
    }
  else if(key == X)
    {
      intKey = 23;
    }
      else if(key == Y)
    {
      intKey = 24;
    }
  else if(Key == Z)
    {
      intKey = 25;
    }
  for( int a = 0; a < str.length(Cip); a = a + 1)
    {


}
char SolveCipher(const char Cip[], char dec[]);
{

}

int main()
{

  return 0;
}

A char is a small integer and in the ASCII table all of the English alphabet letters are ordered: B will be the next integer after A, C will go after B, and so on. This means that you can get intKey with simple maths:

int intKey = key - 'A';

You may also use:

#include "stdafx.h"
#include <iostream>

using namespace std;

#define toDigit(k) (k - 'A')

int _tmain(int argc, _TCHAR* argv[])
{
    cout << toDigit('A') << endl;

    system("pause");
    return 0;
}

In ASCII, A - Z is represented with integers 65 - 90. If you want to set it to 0 - 25 then you just subtract your first value of 65 to give you your value from 0.

Example:

'A' = 65

'A' - 'A' = 0

'B' = 66

'B' - 'A' = 1

And ..............

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM