**Prerequisite : **Turing Machine

**Task :**

Our task is to design a Turing machine to reverse a string consisting of a’s and b’s.

**Examples :**

Input-1 : aabbOutput-1 : bbaaInput-2 : ababOutput-2 : baba

**Approach :**

The basic idea is to read the input from Right to Left and replace Blank(B) with the alphabet and replace the alphabet with ‘X’. When we read all the a’s and b’s replace all the ‘X’ with Blank(B) and we get the required string.

Let us understand this approach by taking the example “aabb”.

- The first task is that we have to take our pointer to the right so that we can read our string from right to left. To do that we read all the a’s and b’s from left to right and when we get first Blank(B) we turn the pointer to left and we get the rightmost character.
- Now there will be two cases –
- The character we get is ‘a’.
- The character we get is ‘b’.

- In this example we get our first character as ‘b’ i.e the last character of aabb. We replace b with ‘X’ and make Blank(B). An extra Blank will automatically append at the end. Our string looks like this –
- Now we have to get our second character. For that, we move the pointer from right to left and move it until we get an ‘a’ or’b’ after ‘X’. In this case, we get ‘b’. Now we repeat the same task, i.e. we replace that ‘b’ with X and move the pointer from that position to right until we get a Blank(B). When we get a Blank(B) we replace it with the character we get in this case ‘b’ and a Black(B) will automatically append at the end. Our string looks like this –
- Now we have to get or the third character. For that, we move the pointer from right to left and move it until we get an ‘a’ or’b’ after ‘X’. In this case, we get ‘a’. Now we repeat the same task, i.e. we replace that ‘a’ with X and move the pointer from that position to right until we get a Blank(B). When we get a Blank(B) we replace it with the character we get in this case ‘a’ and a Black(B) will automatically append at the end. Our string looks like this –
- Similarly we get our last character which is ‘a’ and we perform the same task as describe in the above steps. Our string will look like this –
- Now we have seen that we have traversed all the four-character and we get the character in order “bbaa” which is reverse of “aabb” i.e. after removing all the ‘X’ we get our required string.
- To remove all the ‘X’ we replace all the ‘X’ with Blank(B) i.e. we get 4 Blank(B) after replacing ‘X’ which is equivalent to a single Blank(B). It means we get our final string.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Design a Turing Machine for equal number of a's and b's
- Turing Machine to check whether given string is Even Palindrome or not
- Turing machine for 1's and 2’s complement
- Construct a Turing Machine for language L = {a^n b^m c^nm where n >=0 and m >= 0}
- Difference between Finite Automata and Turing Machine
- Turing Machine for subtraction | Set 2
- Construct Turing machine for L = {a
^{n}b^{m}a^{(n+m)}| n,m≥1} - Construct a Turing Machine for language L = {0
^{n}1^{n}2^{n}| n≥1} - Construct a Turing Machine for language L = {ww
^{r}| w ∈ {0, 1}} - Construct a Turing Machine for language L = {ww | w ∈ {0,1}}
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i>j>k; k ≥ 1} - Turing Machine for addition
- Turing machine for multiplication
- Turing machine for subtraction | Set 1
- Turing machine for copying data
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i*j = k; i, j, k ≥ 1} - Turing Machine as Comparator
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i< j< k; i ≥ 1} - Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i < j < k or i > j > k} - Construct a Turing Machine for a language L = {a
^{i}b^{j}c^{k}| i<j<k or i>j>k} ∩ {a^{i}b^{j}c^{k}| i>j>k or i>j>k}

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.