Multi Whitespace Compressor

Several times we are forced to operate on strings where there are multiple whitespace characters or various white space characters intermixed. Using regular expressions, it is very simple to replace one or more consecutive whitespace characters by a single space character. For example in Python one could do
>>> import re >>> re.replace(r'\s+', ' ', 'rahul\t agrawal') 'rahul agrawal'
For fun I thought to code a function in C to just do the same. Following is my little code
/*
 * multi_space_compressor
 *
 * compresses multiple white space characters to single
 * white space
 *
 */

#include <assert.h> #include <ctype.h> #include <malloc.h> const unsigned int MOFIFY_ORIGINAL = 1<<0; char *multi_space_compressor(char *src, const unsigned int params) {     int last_char_space = 0;     char cur_char;     char *dst;     int i = 0;     if(!(params && MOFIFY_ORIGINAL))     {         /* do a pass to determine the length and allocate the string */         int  i, req_chars = 0;         for(i=0; (cur_char = *(src + i)); i++)         {             if(!isspace(cur_char))             {                 /* non white space */                 req_chars ++;                 last_char_space = 0;             }             else if (!last_char_space)             {                 req_chars ++;                 last_char_space = 1;             }         }     dst = (char *)malloc(req_chars + 1);     assert(dst != 0);     }     else         dst = src;     last_char_space = 0;     i = 0;     for(;(cur_char = *src);src++)     {         if(!isspace(cur_char))         {             /* non white space */             *(dst + i) = cur_char;             last_char_space = 0;             i++;         }         else if(!last_char_space)         {             *(dst + i) = ' ';             last_char_space = 1;             i++;         }     }     *(dst + i) = '\0';     return dst; }
We are given an array containing n numbers. There is no restriction on what the individual number could be. We want to transform this array so that element at any index is replaced by cumulative multiplication of all elements of the array except the one at that index. For example if the array is

A = [5, 6, 7, 8] then after processing it should become A = [6*7*8, 5*7*8, 5*6*8, 5*6*7].

Develop an algorithm to accomplish the above transformation such that the time complexity of the algorithm is O(n) where n is the number of elements in the array.

नमामीशमीशान निर्वाणरूपं विभुं व्यापकं ब्रह्मवेदस्वरूपम् |
निजं निर्गुणं निर्विकल्पं निरीहं चिदाकाशमाकाशवासं भजेङहम् ||१||

निराकारमोंकारमूलं तुरीयं गिराज्ञानगोतीतमीशं गिरीशम् |
करालं महाकालकालं कृपालं गुणागारसंसारपारं नतोङहम् ||२||

तुषाराद्रिसंकाशगौरं गभीरं मनोभूतकोटि प्रभाश्रीशरीरम् |
स्फुरन्मौलिकल्लोलिनी चारुगंगा लसदभालबालेन्दुकण्ठे भुजंगा ||३||


चलत्कुण्डलं भ्रुसुनेत्रं विशालं प्रसन्नाननं नीलकण्ठं दयालम् |
मृगाधीशचर्माम्बरं मुण्डमालं प्रियं शंकरं सर्वनाथं भजामि ||४||

प्रचण्डं प्रकृष्टं प्रगल्भं परेशं अखण्डं अजं भानुकोटिप्रकाशम् |
त्रयः शूलनिर्मूलनं शूलपाणिं भजेङहं भावानीपतिं भावगम्यम् ||५||

कलातिकल्याण कल्पान्तकारी सदा सज्जनान्ददाता पुरारी |
चिदानंदसंदोह मोहापहारी प्रसीद प्रसीद प्रभो मन्मथारी ||६||

न यावद उमानाथपादारविन्दं भजन्तीह लोके परे वा नराणाम् |
न तावत्सुखं शान्ति संतापनाशं प्रसीद प्रभो सर्वभूताधिवासम् ||७||

न जानामि योगं जपं नैव पूजां नतोङहं सदा सर्वदा शम्भुतुभ्यम् |
जरजन्मदुःखौ घतातप्यमानं प्रभो पाहि आपन्नमामीश शम्भो ||८||

रुद्रष्टकमिदं प्रोक्तं विपेण हरतुष्टये |
ये पठन्ति नरा भक्त्या तेषां शम्भुः प्रसीदति ||

|| इति श्री गोस्वामी तुलसीदास कृतं श्री रुद्राष्टकम् संपूर्णं ||

Below is a wonderful composition from a great Saint from India, Mirabai. She forms one of the stalwarts of Hinduism, who kept the masses tied to Hinduism, when there were scathing attacks on Hinduism. She along with other stalwarts of the period like Goswami Tulsidas, Surdas, Chaitanya Mahaprabhu, Narsinh Mehta, Kabirdas, Guru Nanak, Tukaram, Ghasidas, Andal, Eknath, Ravidas, Rahim and countless others who look birth throughout the length and breadth of Bharat, defeated all attempts of destroying Hinduism from the face of Bharat.

हरि तुम हरो जन की पीर।
द्रोपदी की लाज राखी तुम बढायो चीर॥
भक्त कारन रूप नरहरि धरयो आप शरीर।
हिरन्यकश्यप मार लीन्हो धरयो नाहि न धीर॥
बूढत गजराज राखयो कियो बाहर नीर।
दासी मीरा लाल गिरधर दुख जहाँ तहाँ पीर॥

top