Caesar Cipher Extension Question

Hello! I’m working on the Caesar Cipher project at :
(https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-caesar-cipher)

I’ve been able to complete the base project and first extension, but I’m stuck on the second extension.

" The app doesn’t work with symbols, like ! or ? . Skip any symbols in your loop so that they are not encrypted."

I’m not really sure how to skip something and still have it print to console. The best solution I can come up with is an if... else statement that excludes everything not in the array I’ve made, but I don’t think that’s what I’m supposed to do. And if it is what I’m supposed to do, I’d still like to know how to skip a special character without getting rid of it altogether.

My searching has shown me ways to escape special characters in a string, but is that applicable here? Also, do I have to specify every special character individually? I kinda think that if...else would help me here, but I’m not sure how to make it happen.

Here is what I have already:

using System;

namespace CaesarCipher
{
  class Program
  {
    static void Main(string[] args)
    {
      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'};
      
      Console.WriteLine("Please enter your secret message, then hit ENTER:");
      string message = Console.ReadLine().ToLower();


      //something like "if mmessage containse alphabet, move on. Else, please use only letters.
      char[] secretMessage = message.ToCharArray();
      char[] encryptedMessage = new char[secretMessage.Length];

      for (int i = 0; i < secretMessage.Length; i++) 
      {
        char letter = secretMessage[i];
        int index = Array.IndexOf(alphabet, letter);
        int cipher = (index + 3) % alphabet.Length;
        char crypt = alphabet[cipher];
        encryptedMessage[i] = crypt;                         
      }
      
      Console.WriteLine(String.Join("", encryptedMessage));      

    }
  }
}

If I run the program and type in “Hello!” I get the output ‘‘khoorc’’
I would like to instead get the output ‘‘khoor!’’

Please guide me, code oracles!

Hello @omgbano!

You could try adding an if statement that would add the special character in secretMessage to the same index in the encryptedMessage array inside your for loop.

Hi @omgbano

The way I solved it is to add an if statement with the following condition:

if (Array.IndexOf(alphabet,letter)!=-1)

    {

      int newLetterPos = ((Array.IndexOf(alphabet,letter))+3)%26;

      encryptedMessage [i] = alphabet[newLetterPos];

    }

the reason behind that is the following:

If you use the IndexOf method and the system cannot find any match you are looking for (we are looking for any letter that the user put in, through our array, which is the alphabet) it throws “-1”. So the idea was:
If you can find something (Array.IndexOf(alphabet,letter)!=-1)
add +3 to the index,
else
add the char to the array holding the encrypted chars.

here is the whole for loop:

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];

    }

  }