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 −
string encode(vector<string< strs) {
   //code to read strings and return encoded_string;
}
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,
Define a function encode(), this will take an array strs,
ret := empty string
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]
for initialize i := 0, when i < size of strs, update (increase i by 1), do −
ret := ret concatenate size of strs[i]
ret := ret concatenate size of strs[i]
return ret
return ret
Define a function getNext(), this will take x, start, s,
Define a function getNext(), this will take x, start, s,
idx := size of 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 := iCome out from the loop
for initialize i := start, when i < size of s, update (increase i by 1), do −
if s[i] is same as x, then −idx := iCome out from the loop
if s[i] is same as x, then −
idx := i
idx := i
Come out from the loop
Come out from the loop
return idx
return idx
Define method decode this will take s
Define method decode this will take s
Define an array ret
Define an array ret
i := 0
i := 0
n := size of s
n := size of s
while i < n, do −hashPos := getNext('#', i, s)len := (substring of s from index (i to hashPos - i - 1) as integeri := hashPos + 1insert substring of s from index (i to len - 1) at the end of reti := i + len
while i < n, do −
hashPos := getNext('#', i, s)
hashPos := getNext('#', i, s)
len := (substring of s from index (i to hashPos - i - 1) as integer
len := (substring of s from index (i to hashPos - i - 1) as integer
i := hashPos + 1
i := hashPos + 1
insert substring of s from index (i to len - 1) at the end of ret
insert substring of s from index (i to len - 1) at the end of ret
i := i + len
i := i + len
return ret
return ret
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));
}
{"hello", "world", "coding", "challenge"}
Encoded String 5#hello5#world6#coding9#challenge
[hello, world, coding, challenge, ]