
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 −
#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, ]
- Related Articles
- Arduino – base64 encode and decode
- How to encode and decode URl in typescript?
- Encode and decode uuencode files using Python
- How to encode and decode a URL in JavaScript?
- Encode and decode binhex4 files using Python (binhex)
- Encode and decode XDR data using Python xdrlib
- How to Encode and Decode JSON and Lua Programming?
- Encode and decode MIME quoted-printable data using Python
- What is the difference between encode/decode in Python?
- How to encode multiple strings that have the same length using Tensorflow and Python?
- Encode Number in C++
- Decode String in C++
- Decode Ways in Python
- Understanding base64 encode in MySQL?
- Decode Ways II in C++
