• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Number Substitution Generator

So i am trying to develop a real simple app that will take letters entered in a textbox and convert it to numbers in another.  (a or A = 1, b or B = 2).

How would i go about doing this?  I dont need the code (yet) but rather ideas on how it would be done and what methods etc to research in order to learn how to do it.

Thanks!
0
TheRookie32
Asked:
TheRookie32
  • 6
  • 6
  • 4
  • +2
2 Solutions
 
razorback041Commented:
research enums, that would be pretty simple
then utiilize the .toupper or .tolower (depending on your enum)

alternative
hashtables

either will acomplish what you have specified

good luck
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you numbers are in sequence then you could just subtract a value from the characters ASCII representation.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would use a HashTable so that you can associate any number, string, or combination of them with each of your letters regardless of whether the codes have a pattern or not.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TheRookie32Author Commented:
Ok I'll research the above...  I just wanted to do it for fun to generate a 'code' of sorts.  So the word hello in the first textbox would translate to 8-5-11-11-15.

And actually i may need to be adding a dash in between each letter(number) and leave the space between the words so the person trying to read it can know where one letter(number) begins and not have the word hello be: 85111115. :$
0
 
TheRookie32Author Commented:
hmmm... so i dont quite have it.

I need to get some code in there to handle spaces as well.  (which is what the commented if statement was trying to do)

Heres the code for the button push:

Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
        'Get the string of letters
        Dim letters As String = txtCodeInput.Text
        'trim any leading or trailing spaces
        'letters.Trim()
        'Determine the length of the string
        Dim letterCount As Integer = txtCodeInput.TextLength
        'set all the letters to lowercase as in my enums, they are all lowercase
        letters.ToLower()

        Dim sb As New System.Text.StringBuilder
        Dim messageBefore As String
        Dim messageAfter As Integer

        Dim MyEnumVal As LetterNumber
        Dim i As Integer

        For i = 0 To letterCount

            'If i = " " Then
            'sb.Append(" ")
            'Else
                MyEnumVal = CType(i, LetterNumber)
                sb.Append(MyEnumVal)
                sb.Append("-")
            'End If

        Next

        txtOutput.Text = sb.ToString

    End Sub

'my abbreviated enum:

 Public Enum LetterNumber
        'Using this to subsitute the letters for numbers
        a = 1
        b = 2
        c = 3
        d = 4
        e = 5
        f = 6
        g = 7
End Enum

But alls that it is doing is printing the enums in order.. so if i enter cat into the top textbox, I get 0-1-2-3 in the bottom one...  
0
 
razorback041Commented:
its in your loop...you are doing

for i = 0 to lettercount

and then you do

MyEnumVal = CType(i, LetterNumber)
                sb.Append(MyEnumVal)

you are getting the number for i, not your letter

try

MyEnumVal = CType(letters(i), LetterNumber)

hth
0
 
razorback041Commented:
nm...your letters isnt an array...oops...i just assumed

not pretty..but

MyEnumVal = CType(letters.substring(i, 1), LetterNumber)

this isnt tested as i dont have vs open..but i think you can get the picture of what im doing

0
 
TheRookie32Author Commented:
gives me an error: 'Cast from string "a" to type 'Integer' is not valid.'

What i need to do is loop through each character in my string and replace that value with the value in my enums list but i am not achieving that goal yet...  :?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Using a HashTable...

    Private codes As New Hashtable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        codes.Add("a", "1")
        codes.Add("b", "2")
        codes.Add("c", "3")
        codes.Add("d", "4")
        codes.Add("e", "5")
        codes.Add("f", "6")
        codes.Add("g", "7")
        codes.Add("h", "8")
        codes.Add("i", "9")
        codes.Add("j", "10")
        codes.Add("k", "11")
        codes.Add("l", "12")
        codes.Add("m", "13")
        codes.Add("n", "14")
        codes.Add("o", "15")
        codes.Add("p", "16")
        codes.Add("q", "17")
        codes.Add("r", "18")
        codes.Add("s", "19")
        codes.Add("t", "20")
        codes.Add("u", "21")
        codes.Add("v", "22")
        codes.Add("w", "23")
        codes.Add("x", "24")
        codes.Add("y", "25")
        codes.Add("z", "26")
    End Sub

    Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
        Dim letter As String
        Dim letters As String = txtCodeInput.Text.Trim.ToLower
        Dim sb As New System.Text.StringBuilder

        For i As Integer = 0 To letters.Length - 1
            letter = letters.Substring(i, 1)
            If letter = " " Then
                sb.Append(" ")
            ElseIf codes.ContainsKey(letter) Then
                sb.Append(codes.Item(letter) & "-")
            Else
                MessageBox.Show(letter, "No Code Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                sb.Append("{" & letter & "}-")
            End If
        Next

        txtOutput.Text = sb.ToString.Replace("- ", " ").Trim("-")
    End Sub
0
 
TheRookie32Author Commented:
Hey thanks for your post... I am not understanding how the letter is getting changed to a number though as when i step through the code, even when i am at the line of code:
'
txtOutput.Text = sb.ToString.Replace("- ", " ").Trim("-")
'
It appears that the value letter is still "a" and not 1.  Yet it posts 1 to the output message box.  

As soon as it gets to end sub, the value of the letter changes to 1 but it seems that this change would happen earlier. (?)

So it works, just not sure why it does...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The "letter" variable will still have the value of the last letter in the string to be converted because of this line:

    letter = letters.Substring(i, 1)

I assure you that the values inside the StringBuilder itself are not letters, but are in fact numbers, dashes and spaces.

How are you viewing what is inside the StringBuilder?  I can't figure it out...but I don't use the debugger often either....
0
 
Bob LearnedCommented:
Look ma, no Hashtable:

  Public Shared Function ConvertAlpha(ByVal input As String) As String

    Dim output As New System.Text.StringBuilder(input.ToUpper())
    For index As Integer = 1 To 26
      output.Replace(Chr(index + 64), index)
    Next

    Return output.ToString()

  End Function

Test:
  Dim test As String = "This is a test of the radio broadcast system"
  Dim conversion As String = ConvertAlpha(test)

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No HashTable!  That's preposterous!  How can you have any pudding if you don't eat your meat!?...

I chose a HashTable (see my previous comment) "so that you can associate any number, string, or combination of them with each of your letters regardless of whether the codes have a pattern or not."

There are many solutions to this problem...   =)
0
 
TheRookie32Author Commented:
@Idle Mind - I trust this statement "I assure you that the values inside the StringBuilder itself are not letters, but are in fact numbers, dashes and spaces." as the code works!  ;)  But i just get confused that the letter variable doesnt change to a 1.  The output textbox seems to be magically populated with the numbers...

I cant view exactly what is in the stringbuilder, I was just watching the 'letter' variable in the watches window...

@TheLearnedOne - i cant get your code to work...  it returns quite a few numbers instead of 1 number for every letter...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The "letter" variable WON'T ever change to a number.

It is there to simply hold the current letter of the string that we are converting:

    letter = letters.Substring(i, 1)

The line above pulls each letter indivually from the string in the loop.

It is this line that converts the letter to a number:

    sb.Append(codes.Item(letter) & "-")

The "codes.Item(letter)" part returns the number equivalent of the value currently in "letter".  The value of letter is NOT changed though!  The return is immediately concatenated with a dash "-" and stored directly into the StringBuilder "sb".

I hope that makes sense...
0
 
Bob LearnedCommented:
You have 26 letters, so how do you want to represent Z with a single digit?

Bob
0
 
TheRookie32Author Commented:
Ok thanks Idle Mind, i get it now... (sorry)  :$

@LearnedOne - it deosnt get represented with a single digit, it gets represented as 26...
0
 
Bob LearnedCommented:
1) ~IM's code does the same thing, so what do you want to happen

2) What should the result be for:

   "The quick brown fox jumped over the lazy dog"

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hey Bob,

It's buried in the comments a couple posts down.  He wants to see it with dashes "-" in between each number:

    "So the word hello in the first textbox would translate to 8-5-11-11-15."

So...

    Public Shared Function ConvertAlpha(ByVal input As String) As String
        Dim output As New System.Text.StringBuilder(input.ToUpper())
        For index As Integer = 1 To 26
            output.Replace(Chr(index + 64), index & "-")
        Next
        output.Replace("- ", " ")
        Return output.ToString().Trim("-")
    End Function
0
 
Bob LearnedCommented:
Thanks, Mike, that WAS buried deep in the rain forest of Brazil ;)

Bob
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now