#C13060. Reverse First N Characters
Reverse First N Characters
Reverse First N Characters
You are given a string s
and an integer n
. Your task is to reverse the first n
characters of s
. If n
is greater than the length of s
, then return the original string unchanged.
In other words, if we denote the string as:
then the required output is:
$$ \text{result} = \begin{cases} \left(s_1s_2\dots s_{n}\right)^R s_{n+1}s_{n+2}\dots s_{|s|} & \text{if } n \leq |s|, \\ s & \text{if } n > |s|, \end{cases}$$where \((s_1s_2\dots s_n)^R\) denotes the reverse of the first n
characters.
inputFormat
The input consists of two lines:
- The first line is a non-empty string
s
(it can be empty as well). - The second line is an integer
n
.
You should read the input from standard input (stdin).
outputFormat
Output the transformed string after reversing its first n
characters if possible. The result should be printed to standard output (stdout).
abcdef
3
cbadef