One of VBScript’s most powerful features is its string-manipulation functions, some of which are summarized in Fig. 24.19. For a complete list consult the VBScript documenta-tion. VBScript strings are case sensitive. The first character in a string has index 1 (as op-posed to arrays which begin at index 0). [Note: Almost all VBScript string-manipulation functions do not modify their string argument(s); rather, they return new strings containing the results. Most VBScript string-manipulation functions take optional arguments.]
We now present a VBScript program (Fig. 24.20) that converts a line of text into its pig Latin equivalent. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form pig Latin phrases. For simplicity, we use the following algorithm:
To form a pig Latin phrase from an English language phrase, the translation proceeds one word at a time. To translate an English word into a pig Latin word, place the first letter of the English word (if it is not a vowel) at the end of the English word and add the letters “ay.” If the first letter of the English word is a vowel place it at the end of the word and add “y.” Thus, the word “jump” becomes “umpjay,” the word “the” becomes “hetay,” and the word “ace” becomes “ceay.” Blanks between words remain as blanks. Make the following assumptions: the English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters.
Lines 16–42 define the Function procedure TranslateToPigLatin which translates the string input by the user from English to pig Latin. Line 22 calls function Split to extract each word in the sentence. By default, Split uses spaces as delimiters. The condition in line 26 calls functions InStr, LCase and Left to determine whether the first letter of a word is a vowel. Function Left is called to retrieve the first letter in words(k)—which is then converted to lowercase using LCase. Function InStr is called to search the string "aeiou" for the string returned by LCase. The starting index in every string is 1, and this is where Instr begins searching.
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<!--Fig. 24.20: piglatin.html -->
<!-- VBScript String Functions -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<title>Using VBScript String Functions</title>
<script type = "text/vbscript">
Public Function TranslateToPigLatin( englishPhrase )
Dim words ' Stores each individual word
Dim k, suffix
' Get each word and store in words the
' default delimiter for Split is a space
words = Split( englishPhrase )
For k = 0 To UBound( words )
' Check if first letter is a vowel
If InStr( 1, "aeiou", _
LCase( Left( words( k ), 1 ) ) ) Then
suffix = "y"
suffix = "ay"
' Convert the word to pig Latin
words( k ) = Right( words( k ), _
Len( words( k ) ) - 1 ) & _
Left( words( k ), 1 ) & suffix
' Return translated phrase, each word
' is separated by spaces
TranslateToPigLatin = Join( words )
phrase = Document.Forms( 0 ).txtInput.Value
Document.forms( 0 ).txtPigLatin.Value = _
TranslateToPigLatin( phrase )
<form action = ""> Enter a sentence
<input type = "text" name = "txtInput" size = "50" />
<input type = "text" name = "txtPigLatin" size = "70" />
<input type = "button" name = "cmdButton"
value = "Translate" /></p>
Fig. 24.20 Using VBScript string-processing functions
Lines 34–36 translate an individual word to pig Latin. Function Len is called to get the number of characters in words( k ). One is subtracted from the value returned by Len, to ensure that the first letter in words( k ) is not included in the string returned by Right. Function Left is called to get the first letter of words( k ), which is then con-catenated to the string returned by Right. Finally the contents of suffix (either "ay" or "y") and a space are concatenated.
Lines 44–51 define an event procedure for cmdButton’s OnClick event. Line 50 calls function TranslateToPigLatin, passing the string input by the user. The pig Latin sentence returned by TranslateToPigLatin is displayed in a text box (line 49).
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.