Click here to Skip to main content
14,325,629 members
   

C#

 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031625-May-19 22:33
memberMember 1379031625-May-19 22:33 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff25-May-19 23:29
mveBillWoodruff25-May-19 23:29 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031626-May-19 0:23
memberMember 1379031626-May-19 0:23 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff26-May-19 0:50
mveBillWoodruff26-May-19 0:50 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031626-May-19 1:52
memberMember 1379031626-May-19 1:52 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff26-May-19 18:57
mveBillWoodruff26-May-19 18:57 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031626-May-19 19:01
memberMember 1379031626-May-19 19:01 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff26-May-19 21:16
mveBillWoodruff26-May-19 21:16 
That's good to know, and I think posing "code challenges" to yourself is an excellent way to learn !

I have found my students seem to understand Linq more quickly if I explain 'Select as meaning "transform each element, and return a new IEnumerable collection" ... and, 'Where as meaning "select."

Your code:
int offset = '9' + '0';
string test = string.Join("", normal.Select(ch => (ch >= 'a' && ch < 'z') ? (char)(ch + 1) : (ch > 'A' && ch <= 'Z') ? (char)(ch - 1) : (ch == 'z') ? (char)'a' : (ch == 'A') ? (char)'Z' : ((char)(Char.IsDigit(ch) ? offset - ch : ch + 1))));
While this is fine as "proof of concept," I'd ask you to imagine a programming job interview where you submitted your code, or this:
public string Encode(string source)
{
    StringBuilder sb = new StringBuilder();

    int offset = '9' + '0';

    foreach (char ch in source)
    {
        if (Char.IsDigit(ch))
        {
            sb.Append((char) (offset - ch));
            continue;
        }

        if (Char.IsUpper(ch))
        {
            if (ch == 'Z')
            {
                sb.Append('Y');
            }
            else
            {
                sb.Append((char) (ch - 1));
            }

            continue;
        }

        if (ch == 'z')
        {
            sb.Append('a');
        }
        else
        {
            sb.Append((char) (ch + 1));
        }
    }

    return sb.ToString();
}
Which version do you think more likely to get you a job ? Which version do you think you could more easily change if the encoding requirements changed ? Which version do you think another programmer could more easily understand ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

GeneralRe: How to replace every different char with another given char Pin
Member 1379031627-May-19 0:33
memberMember 1379031627-May-19 0:33 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff27-May-19 0:46
mveBillWoodruff27-May-19 0:46 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031627-May-19 0:56
memberMember 1379031627-May-19 0:56 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff27-May-19 2:39
mveBillWoodruff27-May-19 2:39 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031628-May-19 8:29
memberMember 1379031628-May-19 8:29 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031628-May-19 20:19
memberMember 1379031628-May-19 20:19 
GeneralRe: How to replace every different char with another given char Pin
Member 798912229-May-19 4:49
memberMember 798912229-May-19 4:49 
GeneralRe: How to replace every different char with another given char Pin
OriginalGriff26-May-19 1:33
protectorOriginalGriff26-May-19 1:33 
GeneralRe: How to replace every different char with another given char Pin
Member 1379031626-May-19 1:51
memberMember 1379031626-May-19 1:51 
GeneralRe: How to replace every different char with another given char Pin
OriginalGriff26-May-19 5:13
protectorOriginalGriff26-May-19 5:13 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff26-May-19 1:51
mveBillWoodruff26-May-19 1:51 
GeneralRe: How to replace every different char with another given char Pin
OriginalGriff26-May-19 3:09
protectorOriginalGriff26-May-19 3:09 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff26-May-19 22:08
mveBillWoodruff26-May-19 22:08 
GeneralRe: How to replace every different char with another given char Pin
OriginalGriff26-May-19 22:22
protectorOriginalGriff26-May-19 22:22 
GeneralRe: How to replace every different char with another given char Pin
BillWoodruff26-May-19 22:55
mveBillWoodruff26-May-19 22:55 
GeneralRe: How to replace every different char with another given char Pin
Member 798912229-May-19 4:52
memberMember 798912229-May-19 4:52 
GeneralRe: How to replace every different char with another given char Pin
OriginalGriff29-May-19 5:08
protectorOriginalGriff29-May-19 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.