Jump to content
Sign in to follow this  

First Letter of Each Word UDF

Recommended Posts

This is a simple function I ported over from a program I wrote in VB.NET and C#. It takes any text, such as an essay or a letter, and extracts all

the letters that begin each word in their proper order, providing also the absolute position of each letter returned. I find this useful when I want

to encode a message in plain text and then have a program detect those codes with the aid of a script. I can't guarantee you an easy day encoding

phrases according to this scheme, but decoding is really as simple as the function below.

#include <Array.au3>

; #FUNCTION$ ===============================================================================
; Name...........: _StringGetFirstLettersOfWords
; Descriptions...: Returns a continous string of the first letters of all words
;                   in a given string and also provides the positions of each letter,
;                   in an array.
; Parameters.....: $sText - The text contain all the words. Could be a paragraph, could be anythig
;                   $aiPositions - An array to which the letters' positions will be added in
;                   the order of the letters.
; Return Value...: A continous string of all the first letters of all words in the text in their order.
; Author.........: Guido Arbia
; Modified.......:
; Rmearks........:
; Related........:
; Link...........:
; Exaple.........: Yes
; #FUNCTION$ ===============================================================================

Func _StringGetFirstLettersOfWords($sText, ByRef $aiPositions)
    $iPosition = 1
    $iLength = StringLen($sText)
    $iState = 0
    $sResult = ""

    While $iPosition < $iLength
        $sCurrentChar = StringMid($sText, $iPosition, 1)

        Switch $iState
            Case 0
                If StringIsAlpha($sCurrentChar) Then
                    If IsArray($aiPositions) Then _ArrayAdd($aiPositions, $iPosition)

                    $sResult &= $sCurrentChar

                    $iState = 1
            Case 1
                If Not StringIsAlpha($sCurrentChar) Then
                    $iState = 0

        $iPosition += 1

    return $sResult

And here is a simple example.

$message = "a pleasent place looks excellent"
Dim $arr[1]

$word = _StringGetFirstLettersOfWords($message)
MsgBox(0, $word, "p is located at " & $arr[2])

Let me know what you think.


"This then is the message which we have heard of him, and declare unto you, that God is light, and in him is no darkness at all. If we say that we have fellowship with him, and walk in darkness, we lie, and do not the truth: But if we walk in the light, as he is in the light, we have fellowship one with another, and the blood of Jesus Christ his Son cleanseth us from all sin. If we say that we have no sin, we deceive ourselves, and the truth is not in us. If we confess our sins, he is faithful and just to forgive us our sins, and to cleanse us from all unrighteousness. If we say that we have not sinned, we make him a liar, and his word is not in us." (I John 1:5-10)


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...