We have given an array of string and we have to serialize the array of string and deserialize the serialized string.
Examples:
Input : "geeks", "are", "awesome" Output : Serialized String : 5~geeks3~are7~awesome Deserialized String : geeks are awesome Input : "hello", "guys", "whats", "up!!!" Output : Serialized String : 5~hello4~guys5~whats5~up!!! Deserialized String : hello guys whats up!!!
Serialization : Scan each element in a string, calculate its length and append it with a string and a element separator or deliminator (that deliminator should not be present in the string). We append the length of the string so that we know the length of each element.
Deserialized Function : Find the position of the deliminator, then from the position + 1 to length of word we store it in an array as a single element.
Implementation:
C++
// CPP program to serialize and // deserialize the array of string #include <iostream> using namespace std;
// Function to serialized the array of string string serialize(string str[], int ln)
{ string temp = "" ;
for ( int i = 0; i < ln; i++) {
int ln = str[i].length();
temp.push_back( '0' + ln);
temp = temp + "~" + str[i];
}
return temp;
} // Function to deserialize the string void deserialized(string str, string deserialize[], int ln)
{ int len, pos = 0;
string temp = "" ;
int i = 0;
while (pos > -1) {
pos = str.find( "~" , pos + 1);
if (pos > 0) {
len = str[pos - 1] - 48;
temp.append(str, pos + 1, len);
deserialize[i++] = temp;
temp = "" ;
}
}
} // Driver function int main()
{ string str[] = { "geeks" , "are" , "awesome" };
int ln = sizeof (str) / sizeof (str[0]);
string serializedstr = serialize(str, ln);
cout << "Serialized String : " << serializedstr << endl;
string deserialize[ln];
deserialized(serializedstr, deserialize, ln);
cout << "Deserialized String : " ;
for ( int i = 0; i < ln; i++)
cout << deserialize[i] << " " ;
return 0;
} |
Java
/*package whatever //do not write package name here */ import java.io.*;
class GFG {
// Fix The Error // Function to serialized the array of string static String serialize(String str[], int ln)
{ String temp = "" ;
for ( int i = 0 ; i < ln; i++) {
int len = str[i].length();
temp = temp + len;
temp = temp + "~" + str[i];
}
return temp;
} // Function to deserialize the string static void deserialized(String str, String deserialize[], int ln)
{ int len = 0 , pos = 0 ;
String temp = "" ;
int i = 0 ;
while (pos > - 1 ) {
pos = str.indexOf( "~" , pos+ 1 );
if (pos > 0 ) {
len = str.charAt(pos- 1 ) - '0' ;
temp = temp + str.substring(pos + 1 , pos+len+ 1 );
deserialize[i++] = temp;
temp = "" ;
}
}
} public static void main (String[] args) {
String str[] = { "geeks" , "are" , "awesome" };
int ln = str.length;
String serializedstr = serialize(str, ln);
System.out.println( "Serialized String : " + serializedstr);
String deserialize[] = new String[ln];
deserialized(serializedstr, deserialize, ln);
System.out.print( "Deserialized String : " );
for ( int i = 0 ; i < ln; i++)
System.out.print(deserialize[i] + " " );
}
} // This code is contributed by aadityaburujwale. |
Python3
# Python3 program to serialize and deserialize the array of string def serialize(arr):
temp = ""
for i in range ( len (arr)):
ln = len (arr[i])
temp + = str (ln)
temp = temp + "~" + arr[i]
return temp
def deserialize( str ):
deserialize = []
pos = 0
temp = ""
while pos > - 1 :
pos = str .find( "~" , pos + 1 )
if pos > 0 :
len = int ( str [pos - 1 ])
temp + = str [pos + 1 :pos + 1 + len ]
deserialize.append(temp)
temp = ""
return deserialize
# Driver function if __name__ = = "__main__" :
arr = [ "geeks" , "are" , "awesome" ]
serializedstr = serialize(arr)
print ( "Serialized String : " + serializedstr)
deserializeArr = deserialize(serializedstr)
print ( "Deserialized String : " + " " .join(deserializeArr))
# This code is contributed by divyansh2212 |
C#
using System;
using System.Linq;
public class GFG {
// Function to serialized the array of string
public static string Serialize( string [] str)
{
string temp = "" ;
for ( int i = 0; i < str.Length; i++) {
int ln = str[i].Length;
temp += ln.ToString() + "~" + str[i];
}
return temp;
}
// Function to deserialize the string
public static string [] Deserialize( string str)
{
string [] deserialize = new String[str.Length];
int len, pos = 0;
string temp = "" ;
int i = 0;
while (pos > -1) {
pos = str.IndexOf( "~" , pos + 1);
if (pos > 0) {
len = int .Parse(str[pos - 1].ToString());
temp += str.Substring(pos + 1, len);
deserialize[i++] = temp;
temp = "" ;
}
}
return deserialize;
}
static public void Main()
{
string [] str
= new string [] { "geeks" , "are" , "awesome" };
string serializedstr = Serialize(str);
Console.WriteLine( "Serialized String : "
+ serializedstr);
string [] deserialized = Deserialize(serializedstr);
Console.WriteLine( "Deserialized String : "
+ string .Join( " " , deserialized));
}
} // Thi code is contributed by akashish__ |
Javascript
// JavaScript program to serialize and // deserialize the array of string // Function to serialized the array of string function serialize(str) {
let temp = "" ;
for (let i = 0; i < str.length; i++) {
let ln = str[i].length; temp += ln; temp += "~" + str[i];
} return temp;
} // Function to deserialize the string function deserialize(str) {
let len, pos = 0; let temp = "" ;
let deserialize = []; let i = 0; while (pos > -1) {
pos = str.indexOf( "~" , pos + 1);
if (pos > 0) {
len = parseInt(str[pos - 1]); temp += str.substr(pos + 1, len); deserialize[i++] = temp; temp = "" ;
} } return deserialize;
} let str = [ "geeks" , "are" , "awesome" ];
let serializedstr = serialize(str); console.log( "Serialized String : " + serializedstr);
let deserializestr = deserialize(serializedstr); console.log( "Deserialized String : " + deserializestr.join( " " ));
// contributed by akashish__ |
Output
Serialized String : 5~geeks3~are7~awesome Deserialized String : geeks are awesome
Recommended Articles