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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- First string from the given array whose reverse is also present in the same array
- Count of strings in the first array which are smaller than every string in the second array
- Count of anagrams of each string in an array present in another array
- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- Difference between Array and String in Java
- Find original array from encrypted array (An array of sums of other elements)
- Find Array formed by adding each element of given array with largest element in new array to its left
- Array obtained by repeatedly reversing array after every insertion from given array
- Find an element in array such that sum of left array is equal to sum of right array
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Number of sub-strings which are anagram of any sub-string of another string
- Longest string which is prefix string of at least two strings
- Check whether second string can be formed from characters of first string
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Minimize cost of choosing and skipping array elements to reach end of the given array
- Queries to search for an element in an array and modify the array based on given conditions
- Generate array having differences between count of occurrences of every array element on its left and right
- Rearrange array by interchanging positions of even and odd elements in the given array

