CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #20

PROGRAMMING IN PASCAL:
TEXT PROCESSING (continued)

(7.  A Digression from Text Processing:  Arithmetic (continued))

We use "+" both for concatenation of strings and for
addition of integers.  How can we keep these 2 uses
separate?  Easy!  Consider the following test program:

program AddIsNotConcat;

var x, y, z : integer;
      s, t, u : varying [20] of char;

begin

   x := 2; y := 3;
   s := 'hello'; t := 'goodby';
 
   u := x + s;
   {note that this is an attempt to combine an integer
     and a string with "+" and to assign the result to
    a string variable; will it be interpreted as
    addition, or as concatenation?}

    writeln('u = ', u);

    z := x + s;

   {note that this is an attempt to combine an integer
     and a string with "+" and to assign the result to
    an integervariable; will it be interpreted as
    addition, or as concatenation?}

    writeln('z = ', z);

    u := s + t; writeln('u = ', u);
    z := x + y; writeln('z = ', z)

    {these last 2 lines should work in the obvious ways:
      the first concatenates 2 strings and assigns it
    to a string variable; the second adds 2 integers
    and assigns the sum to an integer variable}

end.

To see what happens, copy this program and try
to compile it!

b)    Operations on integers:

    readln(<integer-variable>)    -    an I/P statement
    writeln(<integer>)                -    an O/P statement
 
    tests:

        <integer> = <integer>    -    equality
        <integer> <> <integer>    -    inequality
        <integer> > <integer>    -    greater than
        <integer> < <integer>    -    less than
        <integer> >= <integer>    -    greater than or
                                                    equal to
        <integer> <= <integer>    -    less than or equal

    arithmetic operations:
<integer> := <integer> + <integer>    :    addition
                                  -                   :    subtraction
                                 *                    :    multiplication
                                div            : integer division
                                mod          : integer remainder

where:    15 div 12 = 1 (the quotient when dividing
                                    15 by 12)

and:        15 mod 12 = 3 (the remainder when
                                        dividing 15 by 12)

Note that on a 24-hour clock, 15:00 o'clock = 3 p.m.

c)    Order of operations:
 
*    Use () to disambiguate:

e.g.)    x := 2 + 3 * 4
          writeln(x)

What is the output?  Is x = (2+3) * 4 = 5 * 4 = 20?
                           Or is x = 2 + (3*4) = 2 + 12 = 14?

Try it to see!

The easiest way to get what you want is not to have
to remember rules about which operation gets done
first (*, div, and mod are done first; + and - last),
but rather to use parentheses:

If you want the answer to be 20, write:

    x := (2+3)*4

If you want the answer to be 14, write:

    x := 2 + (3*4)
 

8.    Functions

a)    A function isdef a "black box" that takes I/P
                              & returns O/P
                              such that the same I/P always
                                             yields the same O/P

e.g.)    I/P    O/P
            2        4
            3        6
            10        20
             x        2*x

It will never be the case that you'll have I/P 2 but
some other O/P with this function.

If we call this function "f", we can write the I/O
relationships this way:

    f(2) = 4
    f(3) = 6
    f(10) = 20
    f(x) = 2*x

b)    Or we can consider a function as a set of
        I/O pairs, such that 2 pairs with the same
        I/P must always have the same O/P:

        Using the above example:

        f = {(2,4), (3,6), (10,20), ..., (x, 2*x), ...}

c)    If the "black box" is a "glass box", so that we
    can see the "mechanism" inside that converts
    the I/P into the O/P, we call that mechanism
    an "algorithm", and we can then say:

    A function is computable =def there is an algorithm
                                                that computes its I/O
                                                pairs.
 

9.    Length of a String

a)    length(<string>) is a function that takes a
        <string> as I/P, and returns an integer that
        is its length (i.e., the number of characters
        in <string>) as O/P.

e.g.)    length('hello') = 5
          length(' ') = 1
            *    the I/P here was a blank space!
          length('5') = 1
            *    the single character "5" consists of 1 char!
          length('') = 0
            *    the I/P here was the empty string!

e.g.)    program LengthOfString;
          var  len : integer;
                 str : varying [20] of char;

          begin

            writeln('Input a string:');
            readln(str);

            len := length(str);

            writeln('Your string, ', str, ', has ', len,
                        ' characters.')

          end.

Try this with input:
        hello
        abcdefghijklmnopqrstuvwxyz
 

10.    Substrings

a)    So far, we have seen operations (functions) that
       turn strings into strings, integers into integers,
        and strings into integers:

    name                    I/P               O/P
    + (concat)            >= 2 strings    string
    +,-,*,div,mod        2 integers      integer
    length                    string            integer

Now we'll look at a function that takes as input
a string and 2 integers, and that returns as output
another string.

In our Pascal, it's called "substr".  The book calls it
"copy".

b)    Suppose:    var S,T : varying [20] of char;
       Suppose:    T := 'abcdef'
        Suppose we want S = 'bcd'

        S is a substring of T whose starting character
        is #2, and whose length is 3.

        So:
                S := substr(T, 2, 3)

        will do the trick.

c)    In general:

 substr(<string>, <starting integer>, <length integer>)

is a Pascal function that takes <string> and returns
the substring of it that begins at <starting integer>
and has length <length integer>

Again:

    substr(T,N,L) is a Pascal function that outputs
    the substring of T that begins at position N
    and that has length = L.
 
 


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

file: 111F00/lecturenotes20.27oc00.html