String Commands

Length, Concat, Copy, Pos, Insert, Delete

String Commands

This program both explains and demonstrates Pascal string commands. You may want to run it by reading part and then stepping through the program to that point


Week5Ex1

These examples illustrate the string manipulation commands in pascal.
These are a bit hard to understand at first -- not because they are hard --
but because the way that we have to make computers handle text is so
different from the way that we humans work with words. As you read these
examples work at seeing the logical patterns. How do we manipulate text?

The first example simply gets the last letter of a word and checks to see if
the letter is "s" If it is, the program assumes that the word is plural. If not
the machine "makes it plural" by adding an "s" You can see that this is
excessively simplistic but its distance from linguistic reality is a clue as to
how hard "real" language processing is.


Week5Ex2

This program takes a sentence as input and replaces each instance of the
letter "a" with an *. The "a"s are replaced one at a time from left to right and
the sentence is printed after each replacement.
What happens is:
Use the pos command to find the position of the next "a".
Copy all of the sentence before the "a".
Copy all of the sentence after the "a".
Glue the two pieces togather with an * between them.

Note that the program finds "a" but not "A".
A considerably harder variation of this is to replace every "a" with "aa". Can
you see why this is harder?


Week5Ex2a

This is a version of the previous program except that it uses delete and insert
instead of copy and conact
What happens is:
Use the pos command to find the position of the next "a".
delete the "a".
insert a '*' where the the "a" was.

This approach may seem so much more logical and straightforward than the
previous example that yopu may wonder why I even did it the other way.
The answer is that the process of copying separate parts of a string and then rejoining them is
a very important one. You need to have it in your "bag of tricks."
A considerably harder variation of this is to replace every "a" with "aa". Can
you see why this is harder? You won't have to wait much longer, it is the next example


Week5Ex2b

OK -- now we come to the harder version.. In this case we will
replace each instance of the
letter "a" with an "aa".
so that "aardvark" would become "aaaardvaark"
Did you guess what the problem is?
It is that POS always scans from left to right and finds the first match to the pattern.
In the earlier examples we were taking out the "a"s so POS always found the next one.
Here POS will always find the first one!
Here is what we will do:
we will use the variable firstpart to store the finished part of the sentence (with the "a"s doubled)
-- we will use sent to store the unfinished part. So firstpart will get longer while sent gets shorter

Use the pos command to find the position of the next "a".
Copy all of the sentence before and including the "a".
Glue this to what is already in first part (it is empty to start) and glue on another "a"
Copy all of the sentence after the "a". and make this the new value of sent


Week5Ex3

I hate to give examples that are wrong because the student might
not be paying attention and think that it is right! So pay attention!
Here is a program on which you can practice debugging
You might want to try it before you read the hint at the end


Week5Ex4

Talking Pig Latin is a child's game that distorts English so that it sounds like
a foreign language. Before we can write a program we need to know the
rules.
Rule 1: Move all the letters before the first vowel (a,e,i,o,u) to the end of the
word and ad "ay"
Examples:
pig = igpay
latin = atinlay
thought = oughtthay
strength = engthstray
Rule 2: If the first letter is a vowel don't move anything but add "way" to the
end.
Examples:
add = addway
enough = enoughway
Rule 3: If there is no vowel (usually because there is a "y") don't move
anything but add "ay" to the end.
Examples:
by = byay
try = tryay

All for the Pig Latin programs work the same general way:
Here is the standard case:
Find the first vowel, copy everything before it, copy the vowel and
everything after it, reassemble in reverse order and add "ay"


Week5Ex5

In this variation of pig latin the main program stays the same but the
procedure to find the first vowel is changed.


Week5Ex6

In this variation of pig latin the main program stays the same but the
procedure to find the first vowel is changed again.


Week5Ex7

Given a sentence report how many times each letter appears
note that this works with lower case only
As test data try "a man and a boy"


Week5Ex8

This way of counting letters shows off some special features of Pascal
and it looks easier than the previous version.
But aside from amusement, I wouldn't do it this way because I think most
programmers would find it pretty strange


Rules for writing Pascal statements

General Index to Sample Programs

[ Home ][ Vita ][ Copyright  ][ Copyright  ]