|
-
Jul 9th, 2007, 11:38 AM
#1
[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.
-
Jul 9th, 2007, 11:46 AM
#2
Hyperactive Member
Re: Counting using the alphabet
i would use the character values..
chr(97) = a
chr(98) = b
vb Code:
if value > 122 then ' value for z
chr(97) & chr(value - 25)
i think.
edit. this is confusing me. sorry if i made it worse.
-
Jul 9th, 2007, 11:49 AM
#3
Re: Counting using the alphabet
-
Jul 9th, 2007, 12:27 PM
#4
Hyperactive Member
Re: Counting using the alphabet
vb.net Code:
Public Sub Numbers()
Dim s As String ' String value
Dim v As Integer ' Character increment
Dim x As Integer = -1 ' Used for multiple characters
Dim p As Integer = 97 ' Start of characters.. (97 = a)
For i As Integer = 0 To 200
v = i + p
If (v = 123) Then
x += 1
p -= 27
Else
If x > -1 Then
s = Chr(97 + x) & Chr(v)
Else
s = Chr(v)
End If
Console.WriteLine(s)
End If
Next
End Sub
edit.
works up to zz properly... zz = 728
Last edited by NPassero; Jul 9th, 2007 at 12:33 PM.
-
Jul 9th, 2007, 12:54 PM
#5
Re: Counting using the alphabet
 Originally Posted by nmadd
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.
-
Jul 9th, 2007, 01:08 PM
#6
Re: Counting using the alphabet
You certainly helped me many times, so I'm glad I could finally help you.
-
Jul 9th, 2007, 01:14 PM
#7
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:
Dim myAsci As Integer
myAsci = Asc("a") + 1
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:
Private Function SomeThing(ByVal myString As String, ByVal num As Integer) As String
Dim lastChar As Char = myString.Substring(myString.Length - 1)
Dim asci As Integer = Asc(lastChar) + num
If asci > 122 Then
myString = myString.Substring(0, myString.Length - 1) & "aa"
Else
myString = myString.Substring(0, myString.Length - 1) & Chr(asci)
End If
Return myString
End Function
Last edited by VBDT; Jul 9th, 2007 at 02:04 PM.
-
Jul 9th, 2007, 02:57 PM
#8
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
-
Jul 9th, 2007, 03:16 PM
#9
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|