Official

A - chukodai Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar to problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) (https://atcoder.jp/contests/typical90) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.
「C++入門 AtCoder Programming Guide for beginners (APG4b)」(https://atcoder.jp/contests/APG4b) is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.


In order to get accepted for this problem, you need to write a code that actually operates what is indicated in the problem statement. Specifically, write a program that performs the following operations:

  1. First, receive the string \(S\) and the integers \(a\) and \(b\) from the standard input.
  2. Next, swap the \(a\)-th and the \(b\)-th character of \(S\).
  3. Finally, output the string \(S\) to the standard output.

The input and output required in Step 1 and Step 3 depends on programming language, so you need to grasp the specification of the language you use.

When swapping characters in Step 2, be aware that in most programming languages the indices are numbered from 0; that is,

The first character is treated as the \(0\)-th character, the next as the \(1\)-st, the next as the \(2\)-nd, \(\ldots\).

Also note that, when swapping the values of two variables \(x\) and \(y\), the following procedure does not work in general:

x := y
y := x

This is because assigning to variable \(x\) the value of \(y\) in the first line causes the lost of information of the original value of \(x\). This can be resolved by using a temporary variable \(t\) to store the original value of \(x\) in \(t\).

t := x
x := y
y := t

A sample code for this problem in C++ language is shown below.

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
  string s;
  int a, b;
  cin >> s >> a >> b;
  
  char tmp = s[a-1];
  s[a-1] = s[b-1];
  s[b-1] = tmp;
  
  cout << s << endl;
  
  return 0;
}

posted:
last update: