You are viewing our Forum Archives. To view or take place in current topics click here.
[Fun Contest] Converting Phone Numbers to String value
Posted:

[Fun Contest] Converting Phone Numbers to String valuePosted:

Bighair
  • Powerhouse
Status: Offline
Joined: Sep 26, 201013Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201013Year Member
Posts: 401
Reputation Power: 17
Okay, so a few month ago there was A few of these posted. It allows folk to see how each programmer can have completely different code that does the same thing & also how different/similar different languages are.

So the aim of this is to code a short algorithm that converts a phone number to its string value. For example 982135613 would be converted to "Nine, Eight, Two, One, Three, Five, Six, One, Three".

The code must do this:

Get the input.
Validate the input (Make sure each character is a number)
Convert the numbers to strings.
Output the full number converted to a string value.

When posting your code post the code & a picture of the code working & the language it was coded in. Use the number "092837465" in your screenshot

Here is mine -

Language - Visual Basic.

Proof -
[ Register or Signin to view external links. ]

Code -
Imports System.Console
Module Module1

    Sub Main()
        Dim PhoneNum As String = ""
        Write("Please enter the phone number - ")
        PhoneNum = ReadLine()

        Write(ConvertPhoneNumToString(PhoneNum))
        ReadKey()
    End Sub

    Private Function ConvertPhoneNumToString(ByVal num As String) As String
        'First make sure that it is a number & not a text value
        If Not IsNumeric(num) Then Return "Error - Please only insert text."

        'Break the string value into characters & save
        Dim CharNums() As Char = num.ToCharArray

        'Declare a Variable that will contain the final Value
        Dim OutputString As String = ""

        'Do the converting
        For i = 0 To num.Length - 1
            If CharNums(i) = "0" Then OutputString += "Zero, "
            If CharNums(i) = "1" Then OutputString += "One, "
            If CharNums(i) = "2" Then OutputString += "Two, "
            If CharNums(i) = "3" Then OutputString += "Three, "
            If CharNums(i) = "4" Then OutputString += "Four, "
            If CharNums(i) = "5" Then OutputString += "Five, "
            If CharNums(i) = "6" Then OutputString += "Six, "
            If CharNums(i) = "7" Then OutputString += "Seven, "
            If CharNums(i) = "8" Then OutputString += "Eight, "
            If CharNums(i) = "9" Then OutputString += "Nine, "
        Next

        Return OutputString
    End Function

End Module


Please try comment your work! I'm hoping this is popular and will help get this section a little more active!

The following 1 user thanked Bighair for this useful post:

7en (09-04-2013)
#2. Posted:
Experiment5X
  • TTG Senior
Status: Offline
Joined: Aug 14, 200914Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200914Year Member
Posts: 1,291
Reputation Power: 65
Language: C

Screenshot:
[ Register or Signin to view external links. ]

Code: [ Register or Signin to view external links. ]
#3. Posted:
Bighair
  • Powerhouse
Status: Offline
Joined: Sep 26, 201013Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201013Year Member
Posts: 401
Reputation Power: 17
Experiment5X wrote Language: C

Screenshot:
[ Register or Signin to view external links. ]

Code: [ Register or Signin to view external links. ]


Cool! I Like how you put the numbers zero to Nine in an array!

Keep 'em coming guys!
#4. Posted:
RC4
  • Rising Star
Status: Offline
Joined: Feb 18, 201212Year Member
Posts: 773
Reputation Power: 32
Status: Offline
Joined: Feb 18, 201212Year Member
Posts: 773
Reputation Power: 32
This is were Regex.Replace(); comes in handy...
C#
#5. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Language: PHP

Screenshot:
[ Register or Signin to view external links. ]

Code: [ Register or Signin to view external links. ]
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.