Begin by copying the program skeleton split.adb from the class directory into your new directory. Then use your favorite text editor to open the file, and take a moment to study it, to see how it implements our basic algorithm.
To write a subprogram that performs step 2 of our algorithm, we need some means for that subprogram to send back two pieces of information: the two parts of the string being split.
In Ada, a procedural subprogram is called a procedure:
Subprogram ::= Function | Procedure
Procedure ::= procedure identifier (ParameterDecs) is
Declarations
begin
Statement;
MoreStatements
end identifier;
MoreStatements ::= Statement ; MoreStatements | Ø
As we have seen before, Ada parameters are defined within the
subprogram's first line, between its parentheses.
Ada's procedure parameters are a bit different than its function parameters:
ParameterDecs ::= Ø | ParamDec MoreParamDecs | Ø
ParamDec ::= idList : Mode Type
MoreParamDecs ::= ; ParamDec MoreParamDecs | Ø
idList ::= identifier MoreIds
MoreIds ::= , identifier MoreIds | Ø
Mode ::= in | out | in out | Ø
Type ::= integer | natural | positive | float
| character | string | ...
Recall that Ada provides the String type for storing
sequences of characters (as seen in the main program).
Ada actually provides three different kinds of Strings:
For the sake of simplicity, use the type String to declare parameters named The_String, Part1, and Part2.
As indicated in the preceding productions, Ada procedure parameters should be declared with a mode that designates their relationship to their corresponding parameter:
Using this information, define a subprogram stub named split() that satisfies our problem specification.
To fill in the body of the subprogram, we must be able to select a substring of our parameter the_String. For Ada's fixed-length strings, this is accomplished using the substring operation:
SubStringOp ::= identifier(Start .. Stop) Start ::= PositiveExpression Stop ::= PositiveExpressionThis operation returns the substring of the string named identifier, beginning at index Start, and ending at index Stop. (In Ada terminology, this is called the slice operation.)
For fixed-length arrays, the normal Ada assignment operator (:=) can be used to copy values between two arrays provided the arrays are the same length.
Unfortunately, that is not the case here: parameter First_Part is an array whose size generally speaking different than the size of the substring we wish to assign to it. The sizes of Last_Part and the substring we wish to assign to it are similarly mismatched.
The Ada.Strings.Fixed package contains a Move() procedure that can be used to copy between fixed-length strings of different sizes. Its syntax is:
MoveProcedure ::= Move(FromStr,ToStr);where FromStr is the string we are copying from, and ToStr is the string we are copying to. This Move() procedure, in combination with the substring operation, can be used to set the values of First_Part and Last_Part.
Ada also provides some special array attributes that are useful for our situation:
arrayName'First
can be used to retrieve the index of the first character in an array,
regardless of how a programmer has defined that array.
arrayName'Last
can be used, regardless of how a programmer has defined that array.
arrayName'Length
can be used.
Drawing on these observations, complete the split() stub. If necessary, use one of the available Ada references to look up the syntax details. Don't forget to add Ada.Strings.Fixed to your program's with and uses directives!
Compile and test your code for correctness; then use script to create a file in which you cat your file, compile it, and show that it works correctly for the input string hello. Since our Ada strings are using default indexes (i.e., indexed relative to 1), test using the position values 1, 4 and 6. Save the resulting script file to be concatenated to your submission.
That concludes the Ada part of this lab.
Calvin > CS > 214 > Labs > 06 > Ada