Results 1 to 9 of 9

Thread: [RESOLVED] Counting using the alphabet

  1. #1

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Resolved [RESOLVED] Counting using the alphabet

    Hello all,
    How do I properly increment (counting) a number using the alphabet? For example,
    a + 1 = b
    b + 1 = c
    ......
    z + 1 = aa
    aa + 1 = ab
    .......
    Any help is greatly appreciated.

  2. #2
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: Counting using the alphabet

    i would use the character values..

    chr(97) = a
    chr(98) = b

    vb Code:
    1. if value > 122 then ' value for z
    2. chr(97) & chr(value - 25)

    i think.

    edit. this is confusing me. sorry if i made it worse.

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Counting using the alphabet

    I don't know if this will help:
    http://www.freevbcode.com/ShowCode.asp?ID=5440

  4. #4
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: Counting using the alphabet

    vb.net Code:
    1. Public Sub Numbers()
    2.  
    3.       Dim s As String               ' String value
    4.       Dim v As Integer             ' Character increment
    5.       Dim x As Integer = -1       ' Used for multiple characters
    6.       Dim p As Integer = 97       ' Start of characters.. (97 = a)
    7.  
    8.       For i As Integer = 0 To 200
    9.  
    10.          v = i + p
    11.  
    12.          If (v = 123) Then
    13.             x += 1
    14.             p -= 27
    15.          Else
    16.  
    17.             If x > -1 Then
    18.  
    19.                s = Chr(97 + x) & Chr(v)
    20.  
    21.             Else
    22.  
    23.                s = Chr(v)
    24.  
    25.             End If
    26.  
    27.             Console.WriteLine(s)
    28.  
    29.          End If
    30.  
    31.       Next
    32.  
    33.    End Sub


    edit.
    works up to zz properly... zz = 728
    Last edited by NPassero; Jul 9th, 2007 at 12:33 PM.

  5. #5

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Counting using the alphabet

    Quote Originally Posted by nmadd
    I don't know if this will help:
    http://www.freevbcode.com/ShowCode.asp?ID=5440
    Yeah, that's it... It gave me an idea about the logic behind... Just have to rewrite it using proper VB.Net code
    Thank you.

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Counting using the alphabet

    You certainly helped me many times, so I'm glad I could finally help you.

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Counting using the alphabet

    I believe you can use the "Asc" method to get the caracter's ASCI code and than add one to it and show the total number by Chr method. Here an example:
    vb Code:
    1. Dim myAsci As Integer
    2. myAsci = Asc("a") + 1
    3. Debug.WriteLine(Chr(myAsci))

    for the "aa + 1 = ab" you use the same logic. For example the "a" letter asci is 97 and "z"'s is 121 and any thing between are other letters. You can have a flag that if the characters asci + 1 is less than 122 than return Chr(asci).

    Here is the method
    vb Code:
    1. Private Function SomeThing(ByVal myString As String, ByVal num As Integer) As String
    2.         Dim lastChar As Char = myString.Substring(myString.Length - 1)
    3.         Dim asci As Integer = Asc(lastChar) + num
    4.         If asci > 122 Then
    5.             myString = myString.Substring(0, myString.Length - 1) & "aa"
    6.         Else
    7.             myString = myString.Substring(0, myString.Length - 1) & Chr(asci)
    8.         End If
    9.         Return myString
    10.     End Function

  8. #8

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Counting using the alphabet

    Thank you all who have spent your precious time solving my question... You guys are really helpful

    To NPassero: while I have no doubt that your code would work correctly up to "zz", however I need a more robust solution - a function that takes a string a spits out the next incremented string regardless of the input length. Your solution seems to work with pre-known, fixed string length only.

    To VBDT: Your function does something different than what I expect.... For example, If I plug in "zz", I expect to see the output as "aaa", not "zaa"

    Anyway, this is the solution, in case someone else is also looking to do the same thing as I'm...
    Code:
    Private Function IncrementString(ByVal input As String) As String
            Dim output As String = String.Empty
            Dim length As Integer = input.Length
            Dim curChar As Char = Nothing
            Dim i As Integer = 0
            If String.IsNullOrEmpty(input) Then
                output = "a"
            Else
                For i = length - 1 To 0 Step -1
                    curChar = input(i)
                    If curChar = "z"c Then
                        input = input.Substring(0, i) & "a" & input.Substring(i + 1)
                        If i = 0 Then
                            input = "a" & input
                        End If
                    Else
                        input = input.Substring(0, i) & Chr(Asc(curChar) + 1) & input.Substring(i + 1)
                        Exit For
                    End If
                Next
                output = input
            End If
            Return output
        End Function

  9. #9
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [RESOLVED] Counting using the alphabet

    Gotcha, didn't know the functionality that you were using it for. good work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width