String+Manipulation

ASSIGNMENT #9 STRING MANIPULATION: ORD, CHR, SUCC, PRED, LENGTH, STRINT, STREAL, INTSTR, REALSTR

String manipulation is, in general, a useful method to check for input errors on the part of a user and prevent a program from crashing due to improper input values. For example, if a program has a integer variable, and the user accidentally types a string character, your program will crash unless your taken that eventuality into consideration and taken steps to deal with it such as converting the string into an integer or checking to see whether an integer has been input and outputting an error message to screen if it hasn't.

Here are some useful predefined Turing functions which will assist you in manipulating strings.


 * **Function** || **Definition** ||
 * **ord** || The ord function returns the ASCII code for a single character, i.e., ord ('A') will return 65 ||
 * **chr** || The chr function is the inverse of the ord function. It returns the character value of an ASCII integer, i.e., chr (66) will return "B" ||
 * **succ** || The succ function returns the character following (or succeeding) the parameter character in the ASCII table, i.e., succ ("a") returns "b" ||
 * **pred** || The pred (predecessor) function is the opposite of the succ function, i.e., pred ("b") returns "a" ||
 * **length** || The length function will return the length of a given string, i.e., length ("Louise") will return 6 ||
 * **strint** || The strint function is used to convert a string directly into an integer, i.e., strint ("67") will return the integer 67. ||
 * **intstr** || The intstr function is the opposite of strint, i.e., intstr (66) returns "66" ||
 * **realstr** || The realstr function converts real numbers to strings. i.e., realstr (2.5e1,4) will return "_ _ 25" ||
 * **strreal** || The strreal function is the opposite of realstr, thus strreal ("2.5e1") will return 25. ||
 * **index** || This function is used to find a pattern inside a string. For example, ||

Here are a couple of examples:

var word:string:= "willing" put index (word, "ill")

%will output 2 to the screen since the pattern "ill" starts at the second character of "willing".

This can get kind of interesting since you can substitute one pattern for another, i.e., to change the word "compute" to "complete"

var word: string word:= "computer" word := word (1.. index (word, "u") - 1) + "le" + word (index(word, "u") +1..*) put word


 * Heres a sample application**

% replacing "Mrs" with "Ms" in a line of text var line: string get line const pos:= index (line, "Mrs") if pos not = 0 then line:= line (1.. pos-1) + "Ms" + line (pos + 3) end if put line

In addition, it is worth noting at this point that in OOT, the use of forward and body procedure declarations allows procedures to be called at any time and in any order. However, the body of the procedure must be declared in the same order as the forward declarations. For example:

forward procedure readdata body procedure readdata open: sn, "sample", get assert sn>0 loop get:sn, skip PROCEDURE DEFINITION exit when eof (sn) get:sn, name, skip end loop end readdata

Task

1) Write a program which will accept a string name entry in the form: Francoeur, Jean Pierre and output Francoeur, J.P.

2) Write a program that will accept a word in lower case and then convert it to it's upper case equivalent

3) Write a program that checks to see if the "i before e except after c" rule is followed. Allow the user to enter one word at a time until the word "quit" is entered. Your program should not only spot the error, it should correct it.

Bonus Challenge: Given the following rules, determine if the string MU is derivable from the string MI. Rule 1: If you generate a string whose last letter is I, you may add on a U to the end Rule 2: If you have Mx. Then you may generate Mxx such that from MIU you would get MIUIU or from MIIIU get MIIIUIIIU. Rule 3: If III occurs you may replace it with a U Rule 4: If UU occurs inside a string you may drop it.

The program must display the process by which a solution was found.