RotX Rot = new RotX();PHP Code:
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;
}
}
Rot.Rot(Text,Mod);
I coded this out of boredom, and I'm having a shitload of fun messing with this
Results 1 to 8 of 8
Thread: Rot-X
- 11 Jan. 2011 09:57pm #1
Rot-X
Last edited by Bman; 11 Jan. 2011 at 10:03pm.
- 12 Jan. 2011 01:26am #2
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 10.00
PHP Code: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;
}
}
Removes use of Convert class (Costly for no reason.) Removed unneeded if's.
- 12 Jan. 2011 08:24am #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.
- 12 Jan. 2011 09:28am #4
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 11.74
- 12 Jan. 2011 03:29pm #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:
PHP Code:switch ((int)Old)
{
case (Int)Old >= 'a' && (Int)Old <= 'm':
Return += Convert.ToChar(Grandmother + Mod);
break;
default:
Return += " ";
break;
}
- 12 Jan. 2011 07:48pm #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!
- 13 Jan. 2011 05:23am #7
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 10.61
- 13 Jan. 2011 12:06pm #8