Saturday, February 21, 2015

Creating a Function in LiveCode to Capitalize a Given Word

I'm working on a little project that automatically constructs a narrative - a letter, to be precise - based on some user choices. As I started to build it I immediately found myself needing a way to occasionally capitalize a given word. For example, consider the pronouns he and she, or his and her. My program correctly identifies the gender of the subject of the letter, but sometimes a sentence begins with one of these pronouns and other times the pronoun just occurs somewhere in the sentence. I didn't want to create a variable to hold an all-lowercase version of the pronoun and another to contain a capitalized version. There will certainly lots of other times when a word will need to be capitalized.

I was hoping that LiveCode already had this function, but alas I can't find it. But, it does have the function "upper," which will convert a given text to all upper case. So, I used this to quickly build my own function to capitalize a given word:

function capitalizeWord w
   put char 1 of w into x
   put upper(x) into xupper
   put the number of chars of w into L
   put xupper&char 2 to L of w into y
   return y

end capitalizeWord

The function takes a single word as input and puts it into the local variable w. So, let's imagine that the word entered was "nowhere."

The function begins by dissecting the given word into two parts: the first letter and the rest of the word. The first line takes the first character of w and puts it into another local variable x. So, in the case of "nowhere," x is equal to "n." The next line converts x to uppercase and puts the result in yet another local variable xupper. Great, we now have the first letter of the word capitalized. We now need to join this with the rest of the word

The next line takes note of the total number of characters in the word and puts this number in the variable L. Nowhere has seven characters, so L equals 7. The next line joins xupper ("N") to characters 2 to 7 of nowhere -- N+owhere -- resulting in "Nowhere" and puts this into y, which is then returned to whatever line of code called the function.

To demonstrate how to use this, I created a small stack with two fields and one button:

[ Get the free LiveCode Community version. ]


The left field is labeled "word1" and the field on the right is labeled "word2". The idea is that you type a word into the left field, press the button, and the word is capitalized and shown in the right field. Here's the script of the button "Capitalize":

on mouseUp
   put empty into field "word2"
   
   put line 1 of field "word1" into w1
   put capitalizeWord (w1) into w2
   put w2 into line 1 of field "word2"
   
   put empty into field "word1"
   focus on field "word1"

end mouseUp

The first line simply empties out field "word2." The second line takes whatever is in line 1 of the feld on the left and puts it into the local variable w1 (think of this as word 1).

The next line calls the capitalize function, puts the result into w2, which the next line puts into line 1 of the field on the right.

I added two more lines of code to tidy things up a little. I empty the field on the left and then I make sure that the focus (the flashing vertical text bar) is put into this field.

This function will be pretty darn handy in this and other LiveCode projects!




2 comments:

  1. You might shorten to this:

    function capitalizeWord w
    put upper(char 1 of w) into char 1 of w
    return w
    end capitalizeWord

    ReplyDelete
  2. Thank you, JimL, for your feedback. Yes, your strategy is much more streamlined.

    ReplyDelete