Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

L33t Tr4nsl4t0r (Leet Translator)

5.00/5 (8 votes)
13 Jun 2011CPOL 16.5K  
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:

C#
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:

C#
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.

:)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)