CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #26

PROGRAMMING IN PASCAL:
TEXT PROCESSING (continued)

(16.  To insert new text into an old text (continued))

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)))

d)  How to implement this using Pascal functions:

*    assume that we already know where to insert;
*    assume ptr already moved there;
*    so, want to insert @ ptr:

*    outline:    writeln('INSERT WHAT?');
                    readln(new);
                    text := FirstPart(text, ptr)
                               +
                               new
                               +
                               LastPart(text, ptr)

e)    Need 2 Pascal functions to code "FirstPart"
        & "LastPart":

        function FirstPart(word : string;
                                    loc : integer) : string;
        {I/P = word & integer location}
        {O/P = substring of "word" up to,
                    but not including, char at "loc"}

        begin

            FirstPart := substr(word, 1, loc-1)

        end;

        function LastPart(word : string;
                                    loc : integer) : string;
        {I/P = word & integer location}
        {O/P = substring of "word" starting @ ptr
                    & ending at end of word}

        begin

            LastPart := substr(word, loc,
                                        length(word) - (loc -1))

        end;

f)    Putting it all together in a sketch of the final
        program:

        program InsertTest;

        type string = varying [100] of char;

        var text, command, new : string;
              ptr                          : integer;

        function FirstPart {etc.};
        function LastPart {etc.};

        begin {InsertTest}

            {input text}
            {move ptr}
            {input command}

            if command = 'i'
                then begin {see "outline" above} end
                else {error message}

        end.

g)    Click here for the final program.

h)    How it works:

    1-    The compiler sets aside memory locations
            called "text", "command", and "new"
            (all of type "string") and "ptr" (of type "integer")

    2-    'INPUT TEXT:' is output to the user, prompting
            the user to input the text into which a new
            string will be inserted.

    3-    The user inputs some text, say, 'computer'

    4-    The computer (actually, the "central processing
            unit", e.g., your Pentium chip) stores a copy
            of your input in the memory location "text"

    5-    'INPUT POINTER POSITION:' is output to the
            user, prompting the user to input an integer
            where the new string will be inserted

    6-    The user inputs that integer, say, 3.

    7-    The computer stores a copy of that integer
            in the memory location "ptr".

    8-    'COMMAND:' is output to the user,
            prompting the user to input a text-editing
            command.

    9-    The user inputs a command, say, 'i'

    10-    The computer stores a copy of your input
            into the memory location "command".

    11-    The computer tests whether the contents
            of memory location "command" = the char 'i'

    12-    The test is true, so the "then" part of the
            if-then-else command is executed:

            a-    'INSERT WHAT?' is output to the user,
                    prompting the user to input a string
                    to be inserted into "text"

            b-    The user inputs that new string, say,
                    "science"

            c-    The computer stores a copy of that
                    string in the memory location "new"

            d-    Now begins the computation of
                    the edited string to be stored in "text":

                    i-    To compute FirstPart(text,ptr),
                          the computer checks the declaration
                            of "FirstPart", and sees that it has
                           to set up 3 more memory locations:
                            "word" (of type string),
                            "loc" (of type integer), and
                            "FirstPart" (of type string).

                    ii-    The computer then copies
                            the contents of "text" into "word"
                            & the contents of "ptr" into "loc"

                    iii-    Then it computes
                            substr(word, 1, loc-1) =
                            substr('computer', 1, 3-1) =
                            substr('computer' 1, 2) =
                            'co'

                    iv-    It stores the result in "FirstPart"

                    v-    It then concatenates "FirstPart"
                            and "new", in our case forming
                            'co' + 'science' = 'coscience'.

                    vi-    Then it gets rid of "word", "loc",
                            and "FirstPart", since it no longer
                            needs them.

                    vii-  Then it computes LastPart(text,ptr).
                            To do so, it checks the declaration
                            of LastPart, and sees that it has
                           to set up 3 more memory locations:
                            "word" (of type string),
                            "loc" (of type integer), and
                            "LastPart" (of type string).

                    viii-    The computer then copies the
                                contents of "text" into "word"
                               & the contents of "ptr" into "loc"

                    ix-    Then it computes
                     substr(word, loc, length(word)-(loc-1) =
                     substr('computer', 3, length('computer')
                                                     - (3 -1)) =
                    substr('computer', 3, 8 - (3 -1)) =
                    substr('computer', 3, 8 - 2) =
                    substr('computer', 3, 6) = 'mputer'

                    x-    It stores the result in "LastPart"

                    xi-    It then concatenates "LastPart"
                            onto the end of "FirstPart"+"new",
                            in our case yielding:
                            'coscience' + 'mputer' =
                            'cosciencemputer'

                    xii-    It then gets rid of "word", "loc",
                              and "LastPart", since it no longer
                            needs them.

                    xiii-    It then stores the result of
                                FirstPart(text,ptr)+new
                                + LastPart(text,ptr) in "text"

                    xiv-    Finally, it outputs the contents
                                of "text", so we can see what we
                                did.


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

file: 111F00/lecturenotes26.10nv00.html