Question Caesar Shift

NooBot

New member
Joined
May 19, 2016
Messages
2
Programming Experience
Beginner
Hi everyone!
I am new to C# as well as coding and currently I am trying to challenge myself trying to create a console application that encodes and decodes "Caeser Shift".

Here is my code:


static void Main(string[] args)
{
char[] letters = new char[26] {'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'}; // array of alphabets


string text = Console.ReadLine(); // text input
double shiftnumber;
shiftnumber = Convert.ToDouble(Console.ReadLine()); // the number of times to shift
for (int i = 0; i < text.Length; i++)
{
// This is just where I stuck

}

As you see I am able to separate all the letters of an input text and if I put "Console.WriteLine(text);" and write hello it gives me:
h
e
l
l
o

From this point, what I really want is to use the array containing alphabets to shift my text. I'd really appreciate for help



 
Over complex.

C#:
char originalLetter = 'A';
 int numericLetter = (int)originalLetter;
numericLetter += 1;
char codeLetter = (Char)numericLetter;
Console.WriteLine(codeLetter);

this will change A into B.

so put code like this in a loop
 
Hi, I really appreciate for your reply but I've solved the problem with these codes:

static string Cipher(string text, int shift)
{
char[] alphabet = text.ToCharArray();
for(int i = 0; i < alphabet.Length; i++)
{
char letter = alphabet;
letter = (char)(letter + shift);

if(letter > 'z')
{
letter = (char)(letter - 26);
} else if(letter < 'a')
{
letter = (char)(letter + 26);
}
alphabet = letter;
}
return new string(alphabet);
}
static void Main(string[] args)
{


Console.WriteLine("Caesar Cipher encrypter&decrypter");
Console.Write("Your text: ");
string a = Console.ReadLine();
Console.Write("Shift number: ");
int numb = Convert.ToInt32(Console.ReadLine());
string result = Cipher(a, numb);
Console.WriteLine(result);
}


It is working pretty well; however when I put several texts with whitespaces it also shifts them as well. Like if I put "hello world" and shift it 3 times, the result is "khoor=zruog". It shifts the whitespace and its result displays "=", which I don't want it to be there. So can you help me to solve how can I stop shifting whitespaces?
 
I have included my code for a cipher, I thought you might like to see how I did it. It has a lot going on. it validated the user input for the shift size. It also allows encryption and decryption. It also reads and writes to a file. and exception handling. But the the middle of the try blocks you will see the code that does the ciphering. Sorry the formatting is all cocked up. But if you copy in to visual studio, it can reformat for you.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CeaserCipher
{
class Program
{
static void Main(string[] args)
{
string line = ""; //Holds plain text from the read file
string shift = ""; //Hold user input
string cipher = ""; //Hold the cipher text
int intShift;
int option;

//Select the cipher move
//Validate the input as a number between 0 and 26
while (true)
{
Console.WriteLine("Enter a number between 1 and 25 (The size of the cipher): ");
shift = Console.ReadLine();
Int32.TryParse(shift, out intShift);
if ((intShift > 0) && (intShift < 26))
{
break;
}
}

//Choose encode or decode
while (true)
{
Console.WriteLine("Choose Encode: 1, OR Decode: 2 ");
shift = Console.ReadLine();
Int32.TryParse(shift, out option);
if ((option == 1) || (option == 2))
{
break;
}
}

if (option == 1)
{
//Read a file and encript it
try
{
//Open and Read the file
StreamReader originalText = new StreamReader("Plain Text.txt");
line = originalText.ReadToEnd();

int l;
foreach (char letter in line)
{
l = (int)letter;
l = l + intShift;
cipher = cipher + char.ConvertFromUtf32(l);
}
Console.WriteLine(cipher);
originalText.Close();
//Write the encription to disk
try
{
StreamWriter coded = new StreamWriter("Cipher Text.txt");
coded.WriteLine(cipher);
coded.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("File not found. Ensure \'Cipher Text.txt\' is named correctly and in the correct location.");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Directory not found. Creat the directory and place the file \'Cipher Text.txt\' in it.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
}
Console.ReadLine();
}
catch (FileNotFoundException e)
{
Console.WriteLine("File not found. Ensure \'Plain Text.txt\' is named correctly and in the correct location.");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Directory not found. Creat the directory and place the file \'Plain Text.txt\' in it.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{

}
}
else
{
//Read a file and decript it
try
{
//Open and Read the file
StreamReader originalText = new StreamReader("Cipher Text.txt");
line = originalText.ReadToEnd();

int l;
foreach (char letter in line)
{
l = (int)letter;
l = l - intShift;
cipher = cipher + char.ConvertFromUtf32(l);
}
Console.WriteLine(cipher);
originalText.Close();
//Write the encription to disk
try
{
StreamWriter coded = new StreamWriter("Plain Text.txt");
coded.WriteLine(cipher);
coded.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("File not found. Ensure \'Cipher Text.txt\' is named correctly and in the correct location.");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Directory not found. Creat the directory and place the file \'Cipher Text.txt\' in it.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
}
Console.ReadLine();
}
catch (FileNotFoundException e)
{
Console.WriteLine("File not found. Ensure \'Plain Text.txt\' is named correctly and in the correct location.");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Directory not found. Creat the directory and place the file \'Plain Text.txt\' in it.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{

}

}

}

}
}
 
Back
Top Bottom