String processing: String9
This section contains description of solving the following task:
String9°. Given an even integer N (> 0) and two characters
C1, C2, output
a string that is of length N, begins with C1
and contains alternating characters C1 and C2.
Creating a template and acquaintance with the task
To create a template of the required task one should use PT4Load tool:
{$D+,I+,L+,Q+,R+,S+}
program String9;
uses PT4;
begin
Task('String9');
end.
When the program is launched
you will see the Programming Taskbook window:
Character and string data are enclosed by quotation marks.
It allows to distinguish between numbers and their string representations (for example, 2 and '2'
). Also it gives opportunity to display leading and trailing blank characters of the string.
Initial data input
Before solving the task it is necessary to input initial data in the program:
var
n: integer;
c1, c2: char;
begin
Task('String9');
GetC(c1);
GetC(c2);
GetN(n);
end.
We have tried to input data in wrong order, therefore after the program running
the status bar will contain the error message "Invalid type is used for an input data item."
To correct this error it is enough to change order of input statements:
GetN(n);
GetC(c1);
GetC(c2);
Now data input is performed correctly, but the program does not output results.
Therefore we shall see the following message: "Some data are not output.".
String processing and output
To add characters to a string we shall use "+" operator,
to output the string we shall use the PutS procedure:
var
n, i: integer;
c1, c2: char;
s: string;
begin
Task('String9');
GetN(n);
GetC(c1);
GetC(c2);
s := '';
for i := 1 to n do
s := s + c1 + c2;
PutS(s);
end.
When the program is running you will see the string terminated with a red asterisk. For example,
'a1a1a1a1a1*
It means that the length of the obtained string exceeds the length of a control
string (i.e., the "correct" string). The status bar will contain the message "Wrong solution".
To see the full contents of the obtained string you should
move the mouse cursor to the panel of results. After 1-2 seconds the full text will be displayed
as a hint.
Right solution, its testing and browsing results
The error in the previous program is due to incorrect amount of loop iterations.
Indeed, two symbols are added to string at each iteration. So the string will contain
2n symbols instead of the required n symbols.
To correct the error you should reduce the amount of iterations as follows:
for i := 1 to n div 2 do
After running the changed program we shall see the message "Right solution.
The test 1 of 5", and after 5 test runnings we shall receive the message
"The task is solved!".
Using the PT4Results tool we can browse
information about all test runnings of our program
(the letter "d" means that the task was solved in Delphi Pascal):
String9 d03/05 17:23 Acquaintance with the task.
String9 d03/05 17:25 Invalid type is used for an input data item.
String9 d03/05 17:27 Some data are not output.
String9 d03/05 17:30 Wrong solution.
String9 d03/05 17:34 The task is solved!
|