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.
The solution of the task should be entered in the
Solve procedure, which is defined in the String9 file (this file is created by PT4Load tool,
it has .cs extension for C# and .vb extension for VB.NET):
[C#]
public static void Solve()
{
Task("String9");
}
[VB.NET]
Sub Solve()
Task("String9")
End Sub
When the program is launched
you will see the Programming Taskbook window.
Here is the screenshot of the window for the C# language:
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:
[C#]
public static void Solve()
{
Task("String9");
char c1 = GetChar(), c2 = GetChar();
int n = GetInt();
}
[VB.NET]
Sub Solve()
Task("String9")
Dim c1 As Char = GetChar(), c2 As Char = GetChar()
Dim n As Integer = GetInt()
End Sub
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 two last statements:
[C#]
int n = GetInt();
char c1 = GetChar(), c2 = GetChar();
[VB.NET]
Dim n As Integer = GetInt()
Dim c1 As Char = GetChar(), c2 As Char = GetChar()
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 the operator of string concatenation ("+" for C#, "&" for VB.NET),
to output the string we shall use the Put procedure:
[C#]
public static void Solve()
{
Task("String9");
int n = GetInt();
char c1 = GetChar(), c2 = GetChar();
string s = "";
for (int i = 0; i < n; i++)
s = s + c1 + c2;
Put(s);
}
[VB.NET]
Sub Solve()
Task("String9")
Dim n As Integer = GetInt()
Dim c1 As Char = GetChar(), c2 As Char = GetChar()
Dim s As String = ""
For i As Integer = 1 To n
s = s & c1 & c2
Next
Put(s)
End Sub
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:
[C#]
for (int i = 0; i < n / 2; i++)
[VB.NET]
For i As Integer = 1 To n \ 2
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 "S" means that the task was solved in C#; the letter "B" is used for VB.NET):
String9 S03/05 11:42 Acquaintance with the task.
String9 S03/05 11:44 Invalid type is used for an input data item.
String9 S03/05 11:46 Some data are not output.
String9 S03/05 11:49 Wrong solution.
String9 S03/05 11:54 The task is solved!
|