The Wayback Machine - https://web.archive.org/web/20220309074651/https://www.geeksforgeeks.org/serialize-deserialize-array-string/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Serialize and Deserialize array of string

  • Difficulty Level : Easy
  • Last Updated : 15 Jul, 2017

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.




// 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;
}

Output:

Serialized String : 5~geeks3~are7~awesome
Deserialized String : geeks are awesome

References :
1. https://stackoverflow.com/questions/13271503/converting-array-string-to-string-and-back-in-java
2. https://www.careercup.com/question?id=5684077627703296

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!