How to Decode the string encoded with the given algorithm?


Introduction

Encoding means converting the text into some form of representation, and decoding is the process of converting the encoded text into its original form. Encoding and decoding are two interrelated techniques for transferring information from one form to another. In this tutorial, we implement an approach to decode the string which is encoded with the given algorithm that finds the decoded string using an encoded string encoded with some algorithm.

The string is encoded using the following concept −

For example

string = India
Encoded string = dniIa

In the string "India", the middle character is 'd', save it as an encoded string.

Encoded string = d

Remove the 'd' from the string. The remaining string is 'Inia'.

In this way, we encode a string and decode it in its original form using a decoding algorithm.

Demonstration

Input = Encoded string =  dniIa
Output = Decoded string is "India"

Explanation

The encoded string is "dnila". As per the decoding algorithm, start decoding with the first character of the encoded string, that is 'd'. Save it as a decoded string.

Decoded string = d
Encoded string = nila

Take the character 'n' and save it to the left of the decoded string character.

Decoded string = nd
Encoded string = ila

Take the character 'i' to the right of the decoded string characters.

Decoded string = ndi
Encoded string = la

Take the character 'l' to the left of the decoded string characters.

Decoded string = lndi
Encoded string = a

Take the character 'a' to the right of the decoded string characters.

Decoded string = India

Syntax

  • length() − It is a string class library function, defined in <string> header file. It returns the length of the string in bytes.

string_name.length();

Algorithm

  • Take an encoded string that is encoded with the listed encoding algorithm.

  • Use the 'for' loop to iterate the encoded string.

  • Use the if-else condition to decode the string using the decoding algorithm.

  • Print the decoded string.

Example

Implement an approach in C++ to find the decoded string encoded using the listed algorithm. Used if-else conditions and loops for finding the decoded string.

#include <bits/stdc++.h>
using namespace std;

// user-defined function for decoding the input string
void decodeString(string encodedString, int stringLength) {

   // variable for storing the characters of the decoded string
   char ch[stringLength] = "";
   int bet, p = 1, x;
   if (stringLength % 2 == 1)    // finding the middle character
      bet = stringLength / 2;
   else
      bet = stringLength / 2 - 1;
   ch[bet] = encodedString[0];    // storing the value of the middle character 
   if (stringLength % 2 == 0)
      ch[bet + 1] = encodedString[1];
      
   // x variable denotes the total characters stored in the decoded string
   if (stringLength & 1)
      x = 1;
   else
      x = 2;
      
   //Loop to iterate the string
   for (int a = x; a < stringLength; a += 2) {
      ch[bet - p] = encodedString[a];
      if (stringLength % 2 == 1)  // When the length of the string is not even
         ch[bet + p] = encodedString[a + 1];
      else  // When the string length is even
         ch[bet + p + 1] = encodedString[a + 1];
      p++;  }
   // loop to print each character of the decoded string
   for (int a = 0; a < stringLength; a++)
   cout << ch[a];}
   
//code controller
int main()  {
   string encodedString = "armgmoirnPg"; //encoded string as an input
   int stringLength = encodedString.length();
   
   //calling function to decode the string
   decodeString(encodedString, stringLength);
   return 0;}
Programming

Conclusion

We have reached the end of this tutorial. In this tutorial, we implemented a C++ approach to decode an encoded string. Decoding and encoding are techniques to transfer information from one form to another using some algorithm. It is used in data transmission, data compression, cryptography, and many more.

Developed an encoding algorithm and explained it with an example. Developed a decoding algorithm for finding the original string which is encoded using the described algorithm. Implemented the approach using decoding and encoding algorithms and using conditional statements with loops.

Updated on: 03-Oct-2023

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements