import unittest


def urlify(string, length):
    string_buffer = [None] * length

    # Replacing the spaces by %20
    i = length - 1
    for char in reversed(string):
        if char != ' ':
            string_buffer[i] = char
            i -= 1
        else:
            string_buffer[i - 2:i + 1] = '%20'
            i -= 3

    return ''.join(string_buffer)


class TestUrlify(unittest.TestCase):
    def test_empty_string(self):
        self.assertEquals(urlify('', 0), '')

    def test_spaced_string(self):
        self.assertEquals(urlify('Hello World!', 14), 'Hello%20World!')

    def test_multi_spaced_string(self):
        self.assertEqual(urlify('Hi, my name is Ted.', 27), 'Hi,%20my%20name%20is%20Ted.')


if __name__ == '__main__':
    unittest.main()
