#include <iostream.h>
#include <ctype.h>
#include <string.h>

char lrotor[28], srotor[28], mrotor[28], erotor[28];
//String's for holding the rotor values

char omessage [60] ,emessage [60];
//Strings for the  original and encoded message


void introtor(void);
//This puts the correct starting values into the 3 rotors

void blankarray(char[], int);
//This blanks any charactor array pass the name of the string and the length

void turnsrotor(void);
//This turns the small rotor by 1 charactor

void turnmrotor(void);
//This turns the middle rotor 1 char

void upper(void);
// this converts the original message (omessage) to all uppercase


int main()
{
  
  char ch = ' ', ch2;
  int i = 0, j= 0, k = 1, x = 0;

  cout << "              EnigmA machine "  << endl;

  blankarray(omessage, 60);
  blankarray(emessage, 60);


  introtor();

  cout << "\nPlease enter the secret message" << endl;
  cin.getline(emessage,60);
  
  upper();  

  cout << "\nYour secret Message is : \n" << emessage;
  
  
  
  for(i=0; emessage[i] != '\0'; i++)
    {
      cout << "|" << srotor << "| - small rotor" << endl;     
      cout << "|" << mrotor << "| - middle rotor" << endl;      
      cout << "|" << lrotor << "| - large rotor" << endl;


      cout << "\ndecoding letter:  " << emessage[i] << endl;
      
      cout << "\nfinding letter on large rotor" << endl;

      while (lrotor[j] != emessage[i]) // found the char on the large rotor
	{
	  cout << "*";
	  j++;
	}
      
      omessage[i] = mrotor[j];
      // omessage[i] is the letter on the middle rotor      
      cout << "\nFound letter " << emessage[i] << " on large rotor corresponds to " << omessage[i] << " on middle rotor" << endl; 

      j = 0;

      // we now have to find the letter in omessage[i] on the large rotor
      cout << "Finding " << omessage[i] << " on large rotor" << endl;

      while (lrotor[j] != omessage[i]) 
	{
	  cout << "*";
	  j++;
	}

      omessage[i] = srotor[j];

      cout << "\nFound letter " << emessage[i] << " on large rotor corresponds to " << omessage[i] << " on small rotor" << endl; 

      
      cout << "\nYour decoded message (so far) is: |";
      cout << omessage << "|" << endl;
      
      j = 0;
  
         
      turnsrotor();
   
      if (((i+1) % 27) == 0)
	turnmrotor();
      
    }
  
  
  return 0;
}


void introtor(void)
{
  int i = 0; 
  
  while (i <= 27)
    {
      mrotor[i] = '\0';
      lrotor[i] = '\0';
      srotor[i] = '\0';
      
      i++;
    } 

  i = 0;

  lrotor[i] = ' ';
  mrotor[i] = ' ';
  srotor[i] = ' ';
  i++;
  
  lrotor[i] = 'B';
  mrotor[i] = 'E';
  srotor[i] = 'G';
  i++;
  
  lrotor[i] = 'D';
  mrotor[i] = 'J';
  srotor[i] = 'N';
  i++;
  
  lrotor[i] = 'F';
  mrotor[i] = 'O';
  srotor[i] = 'U';
  i++;

  lrotor[i] = 'H';
  mrotor[i] = 'T';
  srotor[i] = 'A';
  i++;

  lrotor[i] = 'J';
  mrotor[i] = 'Y';
  srotor[i] = 'H';
  i++;

  lrotor[i] = 'L';
  mrotor[i] = 'C';
  srotor[i] = 'O';
  i++;

  lrotor[i] = 'N';
  mrotor[i] = 'H';
  srotor[i] = 'V';
  i++;

  lrotor[i] = 'P';
  mrotor[i] = 'M';
  srotor[i] = 'B';
  i++;

  lrotor[i] = 'R';
  mrotor[i] = 'R';
  srotor[i] = 'I';
  i++;

  lrotor[i] = 'T';
  mrotor[i] = 'W';
  srotor[i] = 'P';
  i++;

  lrotor[i] = 'V';
  mrotor[i] = 'A';
  srotor[i] = 'W';
  i++;

  lrotor[i] = 'X';
  mrotor[i] = 'F';
  srotor[i] = 'C';
  i++;

  lrotor[i] = 'Z';
  mrotor[i] = 'K';
  srotor[i] = 'J';
  i++;

  lrotor[i] = 'A';
  mrotor[i] = 'P';
  srotor[i] = 'Q';
  i++;

  lrotor[i] = 'C';
  mrotor[i] = 'U';
  srotor[i] = 'X';
  i++;

  lrotor[i] = 'E';
  mrotor[i] = 'Z';
  srotor[i] = 'D';
  i++;

  lrotor[i] = 'G';
  mrotor[i] = 'D';
  srotor[i] = 'K';
  i++;

  lrotor[i] = 'I';
  mrotor[i] = 'I';
  srotor[i] = 'R';
  i++;

  lrotor[i] = 'K';
  mrotor[i] = 'N';
  srotor[i] = 'Y';
  i++;

  lrotor[i] = 'M';
  mrotor[i] = 'S';
  srotor[i] = 'E';
  i++;

  lrotor[i] = 'O';
  mrotor[i] = 'X';
  srotor[i] = 'L';
  i++;

  lrotor[i] = 'Q';
  mrotor[i] = 'B';
  srotor[i] = 'S';
  i++;

  lrotor[i] = 'S';
  mrotor[i] = 'G';
  srotor[i] = 'Z';
  i++;

  lrotor[i] = 'U';
  mrotor[i] = 'L';
  srotor[i] = 'F';
  i++;

  lrotor[i] = 'W';
  mrotor[i] = 'Q';
  srotor[i] = 'M';
  i++;

  lrotor[i] = 'Y';
  mrotor[i] = 'V';
  srotor[i] = 'T';
  i++;

  lrotor[i] = '\0';
  mrotor[i] = '\0';
  srotor[i] = '\0';
    

  cout << i << " variables initialised in each rotor. 3 rotors initialised" << endl;
  cout << "The Large rotor is : \n|" << lrotor << "|" << endl;
  cout << "The Middle rotor is : \n|" << mrotor << "|" << endl;
  cout << "The Small rotor is : \n|" << srotor << "|" << endl;

  return;
}


void blankarray(char m[], int len)
{
  int i = 0;
  
  cout << "this array contains \n|" << m << "|" << endl;
  
  cout << "Blanking Array" << endl;

  while (i < len)
    {
      m[i] = '\0';
      
      cout << "*";

      i++;
    }
  
  cout << "\nFinished Blanking Array" << endl;
  cout << "It now contains \n|" << m <<"|" << endl; 

 
  return;
}


void turnmrotor(void) 
{ 
  int k = 1, j = 0;
  
  erotor[0] = mrotor[26];
  
  
    while(k < 27)
      {
	//cout << "erotor[" << k << "] = " << erotor[k] << endl;
	//cout << "mrotor[" << j << "] = " << mrotor[j] << endl;
	erotor[k]= mrotor[j];
	//cout << "Now erotor[" << k << "] = " << erotor[k]<< endl;
	//cout << "Now mrotor[" << j << "] = " << mrotor[j] << endl;
	k++;
	j++;
      }
      
    erotor[27] = '\0';
    
    cout << "middle small rotor used to be : \n|" << mrotor << "|" << endl;
      
    strcpy(mrotor,erotor);
    
    cout << "It now is : \n|" << mrotor << "|" << endl;
    
    return;
}


void turnsrotor(void) 
{ 
  int k = 1, j = 0;
  
  erotor[0] = srotor[26];
  
  
    while(k < 27)
      { 
	//cout << "erotor[" << k << "] = " << erotor[k] << endl;
	//cout << "srotor[" << j << "] = " << srotor[j] << endl;
	erotor[k]= srotor[j];
	//cout << "Now erotor[" << k << "] = " << erotor[k]<< endl;
	//cout << "Now srotor[" << j << "] = " << srotor[j] << endl;
	k++;
	j++;
      }
      
    erotor[27] = '\0';
    
    cout << "small rotor used to be : \n|" << srotor << "|" << endl;
      
    strcpy(srotor,erotor);
    
    cout << "It now is : \n|" << srotor << "|" << endl;
    
    return;
}


void upper(void)
{
  int i = 0;
  cout << "Converting to uppercase" << endl;

  while(emessage[i] != '\0')
    {
      emessage[i] = toupper(emessage[i]);
           
      i++;			    
    }
  
  return;
}










































