2008 Winter Scripting Games

Solution to Sudden Death Challenge Event 6: If At First You Don't Succeed ....

*

If At First You Don’t Succeed ….

To tell you the truth, we kind of outsmarted ourselves with this event. When we first came up with the idea we assumed there would be a very quick, very slick way to solve the problem. When we actually sat down to try to solve the problem, however, we couldn’t figure out what that very quick, very slick solution might be. Therefore, we decided to tackle this event the old-fashioned way: by brute force.

What does that mean? Well, let’s show you the script first, then we’ll explain how it works. As usual, we’ll show you only a VBScript script; needless to say, however, this event could also easily be solved using Windows PowerShell or Perl. Here’s our solution for Event 6 in the Sudden Death Challenge:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Lettercase.txt", ForReading)

strText = objFile.ReadAll
objFile.Close

intLength = Len(strText)

For i = 1 to intLength
    strCharacter = Mid(strText, i, 1)
    If ASC(strCharacter) < 97 Then
        strCharacter = LCase(strCharacter)
    Else
        strCharacter = UCase(strCharacter)
    End If
 
    If IsNumeric(strCharacter) Then
        strCharacter = strCharacter - 1
    End If

    strNewString = strNewString & strCharacter
Next

Wscript.Echo strNewString

OK, now let’s see if we can explain what we did. We start off by defining a constant named ForReading and setting the value to 1; we’ll need that constant in order to open the text file C:\Scrips\Lettercase.txt for reading. Speaking of which, after creating an instance of the Scripting.FileSystemObject object we then use this line of code to open the file Lettercase.txt for, well, for reading:

Set objFile = objFSO.OpenTextFile("C:\Scripts\Lettercase.txt", ForReading)

With the file open, we next use the ReadAll method to read the entire contents of the file into a variable named strText; we then use the Close method to close that file. What that means is that, at the moment, strText is equal to this:

tHE 3119 wINTER sCRIPTING gAMES!

Of course, that also means that we still have plenty of work to do here. Before we can do any of that work, however, we first need to use the Len function to determine the number of characters in the string value strText:

intLength = Len(strText)

Why do we need to know the length of the string (that is, the number of characters in the string)? Well, what we’re going to do is look at each character in the string, one-by-one, and decide whether we need to make that an uppercase character or a lowercase character; we also need to determine whether or not this character is a number and, if so, we need to decrement that value by 1. To do all that we set up a For Next loop that runs from 1 to the number of characters in the string:

For i = 1 to intLength

Inside that loop, we use this line of code to grab the first character in the string strText:

strCharacter = Mid(strText, i, 1)

Next we check to see if the ASCII value of the character is less than 97:

If ASC(strCharacter) < 97 Then

Why do we do that? Well, as it turns out, lowercase letters have ASCII values ranging from 97 (a) to 122 (z). If a character has an ASCII value less than 97 then it must be an uppercase character (assuming, of course, that it’s even a letter in the first place). According to the event instructions, all uppercase letters must be converted to their lowercase equivalent. Therefore, if the character has an ASCII value less than 97 we use this line of code, and the LCase function, to convert the letter to its lowercase counterpart:

strCharacter = LCase(strCharacter)

Note. What if we’re dealing with a number rather than a letter? That’s fine; as far as VBScript is concerned, the lowercase equivalent of the number 2 is simply the number 2. That’s also true for uppercase: the uppercase equivalent of the number 2 is, well, 2.

Now, what if our character has an ASCII value of 97 or higher? In that case we must be dealing with a lowercase letter. As noted in the event instructions, all lowercase letters must be converted to their uppercase equivalent; that’s what this line of code does:

strCharacter = UCase(strCharacter)

That takes care of our uppercase/lowercase issue: characters that were once uppercase are now lowercase, and vice-versa. Of course, that still leaves us with a numbers problem: we need to decrement each number in the string by 1. Just how do we propose doing that?

Well, for starters we use the IsNumeric function to determine whether or not we’re even dealing with a number:

If IsNumeric(strCharacter) Then

If IsNumeric returns True, then we use this line of code to subtract 1 from the value:

strCharacter = strCharacter - 1

That was easy, wasn’t it? All that’s left now is to add this character to a variable named strNewString:

strNewString = strNewString & strCharacter

And then it’s back to the top of the loop, where we repeat the process with the next character in our original string value.

If everything goes according to plan, once we exit the loop strNewString should be equal to this:

The 2008 Winter Scripting Games!

And what do you know: for once everything did go according to plan!


Top of pageTop of page