CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #25

PROGRAMMING IN PASCAL:
TEXT PROCESSING (continued)

16.  To insert new text into an old text:

a)    TDD:    1-    print 'INSERT WHAT?'
                  2-    input string to be inserted into
                          old text; call this string-to-be-
                          inserted "new", & declare it:

                            type string = varying [100] of char;
                            var new : string;

                          So:  we'll have the old text, which
                                we're going to update, stored in
                                memory location called "text",

                                & we'll have the new text to be
                                inserted in "text" stored in
                                memory location called "new"

                                At the end of the process, we'll
                                have the updated text stored in
                                "text" (thus overwriting the old
                                text, which means that we won't
                                be able to undo any mistakes!).

                3-    updated value of "text" will =

                        chars in old "text" up to, but not
                        including, ptr (= location of pointer)

                        +

                        "new"

                        +

                        rest of chars in old "text"

                4-    print updated "text"

e.g. (SWR):    Suppose old "text" = 'abcdefgh',
                    with ptr = 4 (i.e., pointing to 'd')

                    First part of "text" up to ptr = 'abc'

                    Note:  length(first part) =
                              length('abc') = ptr -1 = 3

                 Last part of "text" from ptr to end of "text"
                    = 'defgh'

                    Note:  length(last part) = length('defgh')
                             = length('abcdefgh') - length('abc')
                             = length(text) - length(first part)
                             = length(text) - (ptr -1)
                             =    (in our case) 8 - (4 - 1) = 5

b)    So:    first part of text = substr(text, 1, ptr-1)
                last part of text =
                        substr(text, ptr, length(text)-(ptr-1))

c)    Recall that:

        updated text =
            first part of old text (= substr(text,1,ptr-1))
            +
            new text
            +
            last part of old text
                 (= substr(text, ptr, length(text)-(ptr-1)))

e.g.)    (In this example, I need to show you a blank
           space; that's hard to do in an HTML web
           document, so I'll use the symbol "~" to
           represent a blank space)

        To insert 'are~' (= new)
        in 'How~you?' (= text)
        at ptr = 5:

        updated text
        =  substr('How~you?', 1, 4)
            +
            'are~'
            +
            substr('How~you?', 5, length('How~you?',
                                                        5,
                                                        8 - (5 - 1)))
        =  'How~'
            +
            'are~'
            +
            substr('How~you?', 5, 4)

        =    'How~' + 'are~' + 'you?'
        =    'How~are~you?'

Next:  How to implement this using Pascal functions.
 


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

file: 111F00/lecturenotes25.08nv00.html