Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:31:49+00:00 2026-06-01T17:31:49+00:00

I have this code which shifts the alphabet by a certain amount. The size

  • 0

I have this code which shifts the alphabet by a certain amount. The size of the alphabet is 26. When I enter a larger size shift (for example 22) I get some weird characters displaying. I think I need to mod the ASCII alphabet to 26 to get it working but Im not quite sure which bit to mod.

Basically I need to wrap around the alphabet (once it reaches Z it goes back to letter A) Do I have to create a dictionary for the mod to work (like A = 0… Z = 26) or can I stick with using the normal ASCII table? Here is the code below:

Public Function encrypt(ByVal input As String) 'input is a variable within the funcion
    Dim n as Integer
    Dim i As Integer
    n = key.Text Mod 26 'gets what is in the text box of 'key' and sets it as n
    ' the key is a multiple of 26 so 26 will  = 0

    'need to remove white spaces
    While input.Contains(" ")           'when the input text contains a space
        input = input.Replace(" ", "")  'replaces it with no space.
    End While


    For i = 1 To Len(input)                                     'find the length of the input
        Mid(input, i, 1) = Chr(Asc(Mid(input, i, 1)) + n)       'chr returns the character associated with the specified character code
        '
    Next
    encrypt = input
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:31:50+00:00Added an answer on June 1, 2026 at 5:31 pm

    Look at this code:

    For i = 1 To Len(input)                                     'find the length of the input
        Mid(input, i, 1) = Chr(Asc(Mid(input, i, 1)) + n)       'chr returns the character associated with the specified character code
        '
    Next
    

    String indexes are 0-based. Your first index is 0, not 1! Also, you are assigning to the result of a function call. You need to instead construct a new string.

    You didn’t say, but the way you used the Replace and Contains methods indicates .Net, and if that’s the case, I would do it like this:

    Public Function encrypt(ByVal key As Integer, ByVal input As String) As String 'Don't forget the return type on the function
        key = key Mod 26
        Return New String(input.Replace(" ", "").ToUpper().Select(Function(c) Chr(((Asc(c) + key - Asc("A"c)) Mod 26) + Asc("A"c))).ToArray())
    End Function
    

    Just like that, and it’s almost a one-liner. I can see this works now by calling it this way:

    Encrypt("C"c, "the quick brown fox jumps over the lazy dog")
    Encrypt("D"c, "the quick brown fox jumps over the lazy dog")
    

    The results:

    BPMYCQKSJZWEVNWFRCUXMLWDMZBPMTIHGLWOA
    CQNZDRLTKAXFWOXGSDVYNMXENACQNUJIHMXPB
    

    Look for the results mapped for the word “lazy”, and you will see that the ‘a’ wraps to ‘z’ and ‘y’ correctly, and that the ‘D’ key results are one letter off of the ‘C’ results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class
I have this code which will include template.php file from inside each of these
I have this code which should create a splash image with either no animation
I have this code which is passed to Paypal checkout: <?php foreach ($paypalBasket as
I have this code which can display drop down in div tag of html.
I have this code which I have found from google. It's a adapter for
I have this code which draws a Window and NSView, and have set up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.