You are not logged in. Please login at to post your questions!


ENCMSG- Editorial



Setter- Hasan Jaddouh
Tester- Ivan Safonov
Editorialist- Abhishek Pandey




Strings, Arrays, ASCII system for characters


Given a string $S$ on $N$ characters, we have to perform the following operations-

  • swap adjacent characters. Eg- Swap $1$ with $2$, $3$ with $4$ and so on.
  • swap every alphabet OF THE ORIGINAL STRING with its mirror reverse, i.e. if ASCII value of the character is $K$ , swap it with character of ASCII value $'a'+('z'-K)$. Hence, $'a'$ is replaced by $'z'$, $'b'$ with $'y'$ and so on.


Key to AC- This is a majorly implementation question. Proper practice in these topics is the key to quick AC.

Swapping of the adjacent characters is trivial. The replacement of alphabets can be easily done by realizing that every alphabet is replaced by $'a'+('z'-K)$ counterpart, where $K$ is ASCII value of the character.


This editorial will have only a single section, as its majorly an easy implementation problem. We will be discussing setter's solution owing to its neat implementation.

1.Setter's solution-

The first thing to carry out is the swap operation. Remember the kind of swapping. Let me denote character at index $i$ with $c_i$. We have to swap $c_0$ with $c_1$, $c_2$ with $c_3$ and so on. Familiarity with arrays and strings is needed. You can use a temporary variable for swapping, or perhaps put std::swap function from C++ STL. Both the ways are given in tab below-

View Content

Now all that remains is to do the alphabet replacing. We can do that trivially using the formula given above. An example is given under tab. We will replace $s[i]$ with $s[i]='a'+('z'-s[i]);$

View Content


The codes of setter and tester are pasted below in tabs for you guys to refer if the links dont work. @admin needs some time for linking solutions, hence code was pasted for your convenience.


View Content


View Content

$Time$ $Complexity=$ $O(N)$


1. Some contestants wrote a logic similar to below while coding for swapping part-

for(int i=0;i< n-1;i++){ char c=s[i]; s[i] = s[i+1]; s[i+1] = c; }

Is something wrong there? :o

2. This time, we were able to derive a formula. What if, this wouldnt be possible? What if mapping would had been random, eg - Replace 'a' with 'g', 'h' with 'i', 'b' with also 'g' , or perhaps what if mapping would be given in input? Its easy still! Just make an array of size $26$ and store which alphabet is $i'th$ one mapped to. Meaning, $table[i]$ would store the character to which alphabet with ASCII $'a'+i$ gets mapped to. Now, another version, what if I say that the characters are upto ${10}^{9}$, but only ${10}^{5}$ are used and need to be mapped? I want to bring std::map data structure to your attention. :)

This question is marked "community wiki".

asked 20 Jul '18, 19:32

vijju123's gravatar image

4★vijju123 ♦♦
accept rate: 18%

edited 23 Jul '18, 01:00


  1. you can't use void main(). You have to use int main() with return statement at end.
  2. taking character array of length 100 is wrong because in the end there is null character of each string and any string of length 100 wouldn't fit in the array.
  3. for(j=N;j>=0;j++) is wrong. Nth character would be NULL.

I don't know what logic you used because of poor readability of code but I hope this helps.


answered 27 Jul '18, 00:10

ankush_953's gravatar image

accept rate: 8%

Hi everyone, I have a curiosity here. Instead of swapping the values, can't we dynamically print the output? Here a sample code to explain my point. TestCases on IDE runs well but at the end while submitting I get WA. Any help will be great.


int main()
    int T;
        int B;
        char str[B];
        for(int i=0; i< ((B/2)*2); i++){
                printf("%c",219 - str[i+1]);
                printf("%c",219 - str[i-1]);
        if(B%2 != 0)
            printf("%c\n",219 - str[B-1]);
    return 0;

answered 23 Jul '18, 08:17

anup_gupta's gravatar image

accept rate: 0%

Yes, instead of swapping you can do it directly as well. Take two characters every iteration, print the second character's corresponding alphabet first, then do same for first one.

(23 Jul '18, 14:48) vijju123 ♦♦4★

include <stdio.h>

include <string.h>

void main(){ char S[100] int T scanf("%d",&T); scanf("%d",&N); gets(S); if (N%2==0) { for(j=N;j>=0;j--){ str[j]=str[j--]; goto nextstep; exit(); } } else { nextstep: for (int i = 0; i < N; i++) { temp=str[i]; str[i]=str[i+1]; str[i+1]=temp; break;
} for (int k = 0; k < N; k++) { str[k] = 'z' + 'a'- str[k]; } } printf("%s",str); }

can anyone help...


answered 24 Jul '18, 01:05

imvarnit_1996's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 20 Jul '18, 19:32

question was seen: 711 times

last updated: 27 Jul '18, 00:10