Please help me with the task: C# Beginner 6 The truth Switch statement Encode the message

Hello, I’m working on this task:

We like ciphers at the resistance base. For internal communication, we use a substitutional cipher. The idea is very simple: every letter in the incoming message is substituted with a different symbol. Your task is to partially encode a string message given to your program as an input and output the encoded message. Leave all letters except for the following:

  • F and f: replace with !
  • G and g: replace with @
  • T and t: replace with #
  • H and h: replace with $
  • Y and y: replace with %
  • B and b: replace with ^
    For Example:
    >After seeinG my dark Future, I can’t continue living in the Boring past.
    A!#er seein@ m% dark !u#ure, I can’# con#inue livin@ in #$e ^orin@ pas#.

Here is my code:


namespace SwitchStatement
{
   class SubstitutionCipher
   {
       static void Main()
       {
           string input = Console.ReadLine();
           input = input.ToLower();

           for(int i = 0; i < input.Length; i++)
           {
               switch (input[i])
               {
                   case "f":
                       input[i] = "!";
                       break;
                   case "g":
                       input[i] = "@";
                       break;
                   case "t":
                       input[i] = "#";
                       break;
                   case "h":
                       input[i] = "$";
                       break;
                   case "y":
                       input[i] = "%";
                       break;
                   case "b":
                       input[i] = "^";
                       break;
                   default:
                       break;
               }
           }
       }
   }
}

Can anyone please help me to solve it?

You’re getting array of chars from string “for free”, but it’s read only array. Unfortunately, you can’t just replace any char in a string in C#.
To solve this task you could just print the result char by char in the console.

3 Likes

thank you very much, it’s all clear now. :}

Hej! I had the same problem as Maria did. I tried to make some changes after mentor’s suggestion. Still can’t figure out what else to do to make my code work. Could anyone please help me?

using System;

namespace SwitchStatement
{
    class SubstitutionCipher
    {
        static void Main()
        {
            string message = Console.ReadLine();
            
                        
            
            for (int i = 0; i < message.Length; i++)
            {
                
                switch (message[i])
                {
                    case "f":
                        Console.Write("!"); 
                        break;                       
                    case "g":
                        Console.Write("@");
                        break;
                    case "t":
                        Console.Write("#");
                        break;
                    case "h":
                        Console.Write("$");
                        break;
                    case "y":
                        Console.Write("%");
                        break;
                    case "b":
                        Console.Write("^");  
                        break;             
                    default:
                        Console.Write(message[i]);
                        break; 
                }
            }
        }
    }
       
}
1 Like

the key difference is that your code is only working for lower case letters in the message, while Marias code works for both. The rest is correct :slight_smile:

2 Likes

Sorry, I forgot to mention that I removed the string converting message to lowercase for testing purposes, in both cases the errors I get in the output are:

image

1 Like

since, it’s char use single quotes for all cases. Like
case 'f':

2 Likes

Ah! Didn’t even think it matters! :star_struck: Thanks a lot!

1 Like