VBA – Insert a dash between odd numbers

In the current article, I will present the way to calculate the number of chars in a string with VBA. The inspiration came from the entry exam forHackBulgaria Programming 101v3. The tasks were given some months ago, so I assume it is OK to publish my solutions for them.

The previous task is resolved with VBA here and there.

insertSymbolDB


DashInsert

You have to implement a function with the following signature – dashInsert(num)

  • The argument num is of type integer.
  • The function should return a string

Insert dashes '-' between each two neighboring odd numbers in num.

Don’t count zero as an odd number.

You can use any language you know.

Examples:

dashInsert(99946)
9-9-946
dashInsert(56730)
567-30

Here the task took me a little more time. I have used two loops – one to determine the number of dashes to be inserted and one to insert them. Furthermore, a function IsOdd was created, checking whether a digit is odd. Pretty much, it works flawlessly:

DashInsert

And here comes the code:

Enjoy it!

About

VBA Developer

Tagged with: , , ,