Search code examples
vb.netkeyalphabet

Rearranging the alphabet (VB)


I wanted to basically rearrange the alphabet with a keyword infront as I am doing a simple substitutional cipher. I have kind of worked out the logic of doing that but I am a bit stuck on the coding side.

What I wanted was something like:

Dim key = "keyword"

    For i = 0 to 26

    'insert keyword and put in A to Z after without duplicating characters

    Next


'output: keywordabcfghijlmnpqstuvxz

Solution

  • I think looping through the key is cleaner than looping through the alphabet:

    Dim key as string = "keyword"
    
    Dim alphabet As new StringBuilder("abcdefghijklmnopqrstuvwxyz")
    
    for each c As Char in key
        alphabet.Replace(c.ToString(), Nothing)
    next
    
    return key & alphabet.ToString()
    

    or slightly more efficient change the replace line as follows to avoid scanning all 26 letters of the alphabet on each iteration:

        alphabet.Replace(c.ToString(), Nothing, 0, 1)