Encode and Decode Strings in C++


Suppose we have a list of strings. We have to design an algorithm that can encode the list of strings to a string. We also have to make one decoder that will decode back to the original list of strings. Suppose we have the encoder and decoder installed on these machines, and there are two different functions as follows −

Machine 1 (sender) has the function

string encode(vector<string< strs) {
   //code to read strings and return encoded_string;
}

Machine 2 (receiver) has the function

vector<string< decode(string s) {
   //code to decode encoded_string and returns strs;
}

So, if the input is like {"hello", "world", "coding", "challenge"}, then the output will be Encoded String 5#hello5#world6#coding9#challenge, decoded form [hello, world, coding, challenge, ]

To solve this, we will follow these steps −

  • Define a function encode(), this will take an array strs,

  • ret := empty string

  • for initialize i := 0, when i < size of strs, update (increase i by 1), do −

    • ret := ret concatenate size of strs[i]

  • return ret

  • Define a function getNext(), this will take x, start, s,

  • idx := size of s

  • for initialize i := start, when i < size of s, update (increase i by 1), do −

    • if s[i] is same as x, then −

      • idx := i

      • Come out from the loop

  • return idx

  • Define method decode this will take s

  • Define an array ret

  • i := 0

  • n := size of s

  • while i < n, do −

    • hashPos := getNext('#', i, s)

    • len := (substring of s from index (i to hashPos - i - 1) as integer

    • i := hashPos + 1

    • insert substring of s from index (i to len - 1) at the end of ret

    • i := i + len

  • return ret

Example 

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto< v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Codec {
public:
   string encode(vector<string>& strs) {
      string ret = "";
      for (int i = 0; i < strs.size(); i++) {
         ret += to_string(strs[i].size()) + "#" + strs[i];
      }
      return ret;
   }
   int getNext(char x, int start, string s){
      int idx = s.size();
      for (int i = start; i < s.size(); i++) {
         if (s[i] == x) {
            idx = i;
            break;
         }
      }
      return idx;
   }
   vector<string> decode(string s) {
      vector<string> ret;
      int i = 0;
      int n = s.size();
      while (i < n) {
         int hashPos = getNext('#', i, s);
         int len = stoi(s.substr(i, hashPos - i));
         i = hashPos + 1;
         ret.push_back(s.substr(i, len));
         i += len;
      }
      return ret;
   }
};
main(){
   Codec ob;
   vector<string> v = {"hello", "world", "coding", "challenge"};
   string enc = (ob.encode(v));
   cout << "Encoded String " << enc << endl;
   print_vector(ob.decode(enc));
}

Input

{"hello", "world", "coding", "challenge"}

Output

Encoded String 5#hello5#world6#coding9#challenge
[hello, world, coding, challenge, ]

Updated on: 18-Nov-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements