A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Rot-X

1.3k views · started by Bman ·
#1
Rot-X
using System;
using System.Collections.Generic;
using System.Text;
class RotX
{
public String Rot(String Text, int Mod)
{
Char[] Rotten = Text.ToCharArray();
String Return = String.Empty;
foreach (Char Old in Rotten)
{
int Grandmother = (int)Old;
if (Grandmother == ' ')
{
Return += " ";
}
else if (Grandmother >= 'a' && Grandmother <= 'm')
{
Return += Convert.ToChar(Grandmother + Mod);
}
else if (Grandmother >= 'm' && Grandmother <= 'z')
{
Return += Convert.ToChar(Grandmother - Mod);
}
else if (Grandmother >= 'M' && Grandmother <= 'Z')
{
Return += Convert.ToChar(Grandmother - Mod);
}
else if (Grandmother >= 'A' && Grandmother <= 'M')
{
Return += Convert.ToChar(Grandmother + Mod);
}
}
return Return;
}
}


RotX Rot = new RotX();
Rot.Rot(Text,Mod);

I coded this out of boredom, and I'm having a shitload of fun messing with this :)
#2
using System;
using System.Collections.Generic;
using System.Text;
class RotX
{
public String Rot(String Text, int Mod)
{
String Return = String.Empty;
for (Int32 i = 0; i < Text.Length; i++)
{
Return += ((Text[i] >= 'a' && Text[i] <= 'm') || (Text[i] >= 'A' && Text[i] <= 'M')) ? (Char)((Int32)Text[i] + Mod) : (((Text[i] >= 'm' && Text[i] <= 'z') || (Text[i] >= 'M' && Text[i] <= 'Z')) ? (Char)((Int32)Text[i] - Mod) : (Char)Text[i]);
}
return Return;
}
}[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]


Same code;
Removes use of Convert class (Costly for no reason.) Removed unneeded if's.
#3
Don't listen to Chris. It's bad practice to use long winded ternary statements within long winded ternary statements ;)

Best practice in your example would have been to use a switch statement.
#4
A switch statement for what is 52 possible cases? O.o GLuck, unless there is a method to have a case that covers a range of values, in which...mkay didn't know there was one.
#5
Yea alex is partially correct.

Dont do what chris did, its over complicated for no reason. In C# the visual studio compiler will optimize your code for you. No need for you to be all confused with all the ternary.

Secondly, you cant put something like this in a switch unless you make the switch all weird and stuff if you know what i mean. The reason you cant is because, for example:

switch ((int)Old)
{
case (Int)Old >= 'a' && (Int)Old <= 'm':
Return += Convert.ToChar(Grandmother + Mod);
break;

default:
Return += " ";
break;
}


You cant compare a bool ( the first case statement ) to an Int ((int)old), you will get an error. Though it is possible to put it in a switch, its more work for no real reason.
#6
Thanks for the feedback :) I do see what chris has done is overly complicated, I just took the simple way and made an if statement. Thanks, all of you!
#7
Asydix wrote:
Thanks for the feedback :) I do see what chris has done is overly complicated, I just took the simple way and made an if statement. Thanks, all of you!


Even if you extract what I did, you only needed 2 ifs. Not 4.
#8
Chris wrote:
Even if you extract what I did, you only needed 2 ifs. Not 4.


I know. I could use a logical or statement :)