r/badcode • • Jul 16 '19

[deleted by user]

[removed]

21 Upvotes

25 comments sorted by

View all comments

2

u/dodecakiwi Jul 17 '19 edited Jul 19 '19

I'm going to use Excel to get a column for the letter and then offset that cell by the input shift. Then get the encrypted value from that cell using an Excel formula.

string ceasar(string s, int i)
{
	string ceasar_string = "";

	Excel.Application excel = new Excel.Application();
	excel.Visible = false;
	Excel.Workbook wb = excel.Workbooks.Add();
	Excel.Worksheet ws = wb.Worksheets.Add();

	foreach (var letter in s.Select(x => x.ToString()))
	{
		if (Char.IsLetter(letter[0]))
		{
			string start_col = "A" + letter;
			Excel.Range start_cell = ws.Cells[1, start_col];
			int next_column = start_cell.Column + i;
			Excel.Range cipher_cell = ws.Cells[1, next_column];
			cipher_cell.Formula = "=SUBSTITUTE(ADDRESS(1,COLUMN(),4),\"1\",\"\")";
			string value = cipher_cell.Value.ToString();

			if (!Char.IsUpper(letter[0]))
				value = value.ToLower();

			ceasar_string = ceasar_string + value[value.Length - 1];
		}
		else
		{
			ceasar_string = ceasar_string + letter;
		}
	}

	wb.Close(false);
	excel.Quit();

	return ceasar_string;
}

4

u/[deleted] Jul 18 '19

I appreciate you spelling "Caesar" wrong in your variable names. It really completes the motif.

1

u/sac_boy Jul 19 '19

Flashbacks to my internship in 2002, dark flashbacks indeed