#K81947. Reverse Alphabetic Characters
Reverse Alphabetic Characters
Reverse Alphabetic Characters
You are given a string that may contain any printable ASCII characters. Your task is to reverse the positions of all alphabetic characters in the string, while keeping all other characters (such as digits, punctuation, and spaces) in their original positions. Use a two-pointer approach to swap the alphabetic characters.
Remark: Please note that only alphabetic characters ('a'-'z' and 'A'-'Z') are to be reversed. All other characters must remain in the same position.
For example, for input a,b$c
, the output should be c,b$a
.
The reversal logic can be described mathematically as follows. Let \( A = \{a_i \mid s_i \text{ is an alphabetic character}\} \) be the sequence of alphabetic characters extracted in order from the string. Then, form a new string \( s' \) such that for every index \( i \) in \( s \):
[ s'_i = \begin{cases} \text{the last element of } A \text{ (and then remove it from } A\text{)} & \text{if } s_i \text{ is an alphabetic character}, \ s_i & \text{otherwise.} \end{cases} ]
inputFormat
The input consists of a single line containing a string \( s \) (which may include spaces and other printable ASCII characters).
outputFormat
Output the transformed string after reversing only its alphabetic characters.
## samplea,b$c
c,b$a