Caesar Cipher Extension Method

Hi,
There is already a “solved” post for this, but unfortunately, the creator didnt post the solution. Hence, I had to open a new one.

I am dealing with the following project. I want to wrap the code into a method and call it, which is the last point of this project, 14. the extension.
Caesar Cipher

This is my original code which works fine, maybe not perfect, but lets leave it for now:

namespace CaesarCipher
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Enter your secret message: ");

      char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
      
      string msgMod = (Console.ReadLine()).ToLower();
      char [] secretMessage = msgMod.ToCharArray();
      char[] encryptedMessage = new char[secretMessage.Length];



      for (int i=0; i<secretMessage.Length; i++)
      {
        char letter = secretMessage[i];

        if (Array.IndexOf(alphabet,letter)!=-1)
        {
          int newLetterPos = ((Array.IndexOf(alphabet,letter))+3)%26;
          encryptedMessage [i] = alphabet[newLetterPos];
        }
        else
        {
          encryptedMessage [i] = secretMessage[i];
        }


      } 
      string msgString = String.Join("",encryptedMessage);
      Console.WriteLine(msgString);
    }
  }
}

now I modified the code:
what I did:
-wrapped the code into a method (or at least tried)
-used void as there is no calculation ( I also tried static string Encrypt(string msg) / and add return Console.WriteLine(msgString);
-I added string msg = Console.ReadLine(); outside the method (can it be inside, I think I need to solve my main issue)
-I split string msgMod to tring msg = (Console.ReadLine()).ToLower(); string msgMod = msg.ToLower();

using System;

namespace CaesarCipher
{
  class Program
  {
    static void Main(string[] args)
    {
      
      Console.WriteLine("Enter your secret message: ");
      string msg = Console.ReadLine();          
      Encrypt(msg);

      
    static void Encrypt(string msg)
    {
      char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
      
      string msgMod = msg.ToLower();
      char [] secretMessage = msgMod.ToCharArray();
      char[] encryptedMessage = new char[secretMessage.Length];



      for (int i=0; i<secretMessage.Length; i++)
      {
        char letter = secretMessage[i];

        if (Array.IndexOf(alphabet,letter)!=-1)
        {
          int newLetterPos = ((Array.IndexOf(alphabet,letter))+3)%26;
          encryptedMessage [i] = alphabet[newLetterPos];
        }
        else
        {
          encryptedMessage [i] = secretMessage[i];
        }


      } 
      string msgString = String.Join("",encryptedMessage);
      Console.WriteLine(msgString);
            
   }
     
    }
  }
}

Can someone help? Thanks in advance :slight_smile:

Hello! Consider putting your Encrypt method outside of the main function, and before it:

static void Encrypt() {
/*code*/
}
static void Main(string[] args){
/*call the encrypt function*/
}

The Main method is only meant to be for calling functions and other code that is directly executed. All other code (including other classes, interfaces, methods, etc) should be declared outside of the Main function.

I hope this helps!

1 Like

Thanks a lot!! Works without issues…Looked up several times in the previous lessons but didnt relize that…
Here s my correct code for others:

using System;

namespace CaesarCipher
{
  class Program
  {
    static void Main(string[] args)
    {
      
      Console.WriteLine("Enter your secret message: ");
      string msg = Console.ReadLine();          
      Encrypt(msg);

    } 

    static void Encrypt(string msg)
    {
      char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
      
      string msgMod = msg.ToLower();
      char [] secretMessage = msgMod.ToCharArray();
      char[] encryptedMessage = new char[secretMessage.Length];



      for (int i=0; i<secretMessage.Length; i++)
      {
        char letter = secretMessage[i];

        if (Array.IndexOf(alphabet,letter)!=-1)
        {
          int newLetterPos = ((Array.IndexOf(alphabet,letter))+3)%26;
          encryptedMessage [i] = alphabet[newLetterPos];
        }
        else
        {
          encryptedMessage [i] = secretMessage[i];
        }


      } 
      string msgString = String.Join("",encryptedMessage);
      Console.WriteLine(msgString);
            
   }        
  }
}