CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #24

PROGRAMMING IN PASCAL:
TEXT PROCESSING (continued)

(13.  "index" (continued))

a)    Recall that "index" takes 2 strings as input, and
       returns an integer:

    e.g.)    index('abcdef', 'cdef') = 3
              index('abcbc', 'bc') = 2
              index('a', 'b') = 0

    It works as if it were declared thus:

function index (word, subword : string) : integer;
begin
     if "subword" is a substring of "word"
        then index := the position of the 1st character
                             of the 1st occurrence of "subword"
                            in "word"
        else index := 0
end;

b)    An example of "index":  Pig Latin

    "Pig Latin" is an American children's game
    in which English words are changed so that
    they (sort of) sound like Latin:

    "pig" becomes "igpay"
    "latin" becomes "atinlay"
    "apple" becomes "appleway"

    I.e., if the English word begins with a consonant,
    then the Pig Latin version is formed by moving
    the first letter to the end of the word, and
    appending "ay".

    But if the English word begins with a vowel,
    then the Pig Latin version is formed by appending
    "way" to the end of the word.

    For a Pascal program that does this, click here.

    Note how "index" is used to test if the English
    word begins with a vowel.

    Note also the use of functions to make the program
    more readable; the general algorithm used is this:

    if 1st char = vowel,
        then concat 'way' to end of English word,
        else {the 1st char = consonant, so:}
                concat AllButFirstLetter to FirstLetter to 'ay'
 

14.  Designing & Coding a Simple Text Editor

a)    Want to allow user to:    type in text
                                            modify text
                                                (e.g., insert, delete...)
                                            print text

b)    For a demo, at the UBUnix prompt, type
        (or copy & paste):

   /ubfs/user/r/a/rapaport/ub/solaris/cse111editor

        Or click here for a file containing a demo.

c)    TDD:    Look at big picture first.
                  Then focus in on parts of the big picture.

       SWR:    For each such part, do:
                   *    Look at the part as if it were a big
                        picture.
                   *    Then focus in on its parts
                   until everything is done.
 

15.  Pointer

a)    So:  want to insert, delete, change, etc.,
              & need a pointer to point to where to do
                 these things.

b)    Assume that the string to be manipulated (edited)
       is already contained in a memory location called:
 
            text

        which is initially empty.

        i.e.)    type string = varying [100] of char;
                  var text : string;

c)    So:  Need a pointer to point to where changes
            should be made
            *    sort of like Pico's cursor

e.g.)    to insert 'xyz' in 'abcdef' before the 'd':

            before            after

            abcdef            abcxyzdef
                 ^                    ^
                 ptr                  ptr

            Note that the pointer initially points to 'd'
            but afterwards points to 'x'.

            In reality, it is pointing to the same location,
            namely, the 4th position!

e.g.)    to delete 2 chars starting at ptr:

                before            after
 
                abcxyzdef        abczdef
                     ^                     ^
                    ptr                    ptr

            Again, note that ptr always points to,
            in this case, the 4th position.

d)    Implementation of pointer:

        An integer representing the position of the
        pointer:

         var ptr : integer;

e)    To move pointer:

        1.    print "POINT TO WHAT?"
        2.    input a string, & store it in "target"
        3.    put the follwoing information into the
                integer location called "ptr":
               *    the position of the 1st char of "target"
                    in "text"
                *    use  ptr := index(text, target)

f)    Implementation (SWR):

        {assume some text is stored in "text"}
        writeln('POINT TO WHAT?');
        readln(target);
        ptr := index(text, target)

g)    Test this by embedding it in a program that is
        a fragment of the full editor;

        Click here to see it.
 


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes24.07nvc00.html