65.9K
CodeProject is changing. Read more.
Home

L33t Tr4nsl4t0r (Leet Translator)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (8 votes)

Jun 7, 2011

CPOL
viewsIcon

16509

This is an alternative to "L33t Tr4nsl4t0r (Leet Translator)".

Way too much code. Using arrays of characters, your switch constructs can be reduced to just a few lines of code, like so:

switch (c)
         {
           case 'a': sb.Append("4"); break;
           case 'e': sb.Append("3"); break;
           case 'i': sb.Append("1"); break;
           case 'o': sb.Append("0"); break;
           case 'A': sb.Append("4"); break;
           case 'E': sb.Append("3"); break;
           case 'I': sb.Append("1"); break;
           case 'O': sb.Append("0"); break;
           default: sb.Append(c); break;
         }

becomes:

string rep="43104310";
int index= "aeioAEIO".IndexOf(c);
if (index<0) sb.Append(c);
else sb.Append(rep[index]);

where rep is used to further improve readability.

:)