Programming Taskbook

Russian

E-mail:

Password:

User registration   Restore password

1000 training tasks on programming

©  M. E. Abramyan, 1998–2011

 

Examples | C++ | String processing

PrevNext


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 function, which is defined in the String9.cpp file (this file is created by PT4Load tool):

void Solve()
{
  Task("String9");
}

When the program is launched you will see the Programming Taskbook window:

Character and string data are enclosed by single and double quotation marks respectively. 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:

void Solve()
{
  Task("String9");
  int n;
  char c1, c2;
  pt >> c1 >> c2 >> n;
}

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 variables in the last statement:

  pt >> n >> c1 >> 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 creation and output

To store the string we shall use a variable of the string type. Note that to use this type the program must contain the following statement:

using namespace std;

But it is not necessary to add this statement in the String9.cpp file because it is contained in the pt4.h file, which is included in the String9.cpp file.

To add characters to a string we shall use "+" operator, to output the string we shall use the pt stream:

void Solve()
{
  Task("String9");
  int n;
  char c1, c2;
  pt >> n >> c1 >> c2;
  string s = "";
  for (int i = 0; i < n; i++)
    s = s + c1 + c2;
  pt << s;
}

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.

Remark. The red asterisk can appear also if you output incorrect numeric data. For example, if the expected integer is in the range 1 to 99, then in case of output the result value 1000 it will be displayed as 1*.

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 (int i = 0; i < n / 2; i++)

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 "c" means that the task was solved in C++):

String9   c03/05 17:48 Acquaintance with the task.
String9   c03/05 17:50 Invalid type is used for an input data item.
String9   c03/05 17:52 Some data are not output.
String9   c03/05 17:54 Wrong solution.
String9   c03/05 17:58 The task is solved!

Remark. To solve the task we can also use the char* type as follows:

void Solve()
{
  Task("String9");
  int n;
  char c1, c2;
  pt >> n >> c1 >> c2;
  char* s = new char[n + 1];
  for (int i = 0; i < n / 2; i++)
  {
    s[2 * i] = c1;
    s[2 * i + 1] = c2;
  }
  s[n] = 0;
  pt << s;
  delete[] s;
}

PrevNext

 

 

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
04.06.2011