Bloomberg LP Interview Question
Software Engineer / Developersbool reverseWords( char str[] ){
char *buffer;
int tokenReadPos, wordReadPos, wordEnd, writePos = 0;
/* Position of the last character is length - 1 */
tokenReadPos = strlen(str) - 1;
buffer = (char *) malloc(tokenReadPos + 2);
if( !buffer )
return false; /* reverseWords failed */
while( tokenReadPos >= 0 ){
if( str[tokenReadPos] == ' ' ){ /* Non-word characters */
/* Write character */
buffer[writePos++] = str[tokenReadPos--];
} else { /* Word characters */
/* Store position of end of word */
wordEnd = tokenReadPos;
/* Scan to next non-word character */
while( tokenReadPos >= 0 && str[tokenReadPos] != ' ' )
tokenReadPos--;
/* tokenReadPos went past the start of the word */
wordReadPos = tokenReadPos + 1;
/* Copy the characters of the word */
while( wordReadPos <= wordEnd ){
buffer[writePos++] = str[wordReadPos++];
}
}
}
/* null terminate buffer and copy over str */
buffer[writePos] = '\0';
strcpy(str, buffer);
free(buffer);
return true; /* ReverseWords successful */
}
//Output: final string : abc__def_987654321_xyz_123abc_hi
#include <iostream>
#include <string.h>
using namespace std;
//!function to reverse string from strt to end
void reverseStr(string &s, int strt, int end) {
while(strt<end){
s[strt] = s[strt]+s[end];
s[end] = s[strt]-s[end];
s[strt] = s[strt]-s[end];
++strt;--end;
}
}
void reverseBuckets(string &str){
int start=0,end=0,tmp=0,sz = str.size();
size_t pos;
while(end<sz){
pos = str.find_first_of("_",start);
if(pos>0)
if(pos>=sz && end<sz)
end = sz;
else
end = pos;
reverseStr(str,start,end-1);
if(end>=sz)
break;
pos = str.find_first_not_of("_",end);
if(pos>0){
end = pos;
start=end;
}++end;
}
}
int main(){
string s = "cba__fed_123456789_zyx_cba321_ih";
reverseBuckets(s);
cout<<"final string : "<<s<<endl;
return 0;
}
If we're allowed extra space, we can keep reading the string (and pushing each character into a stack) until we encounter a space.. at this point, we replace the characters in the address pointed by a pointer ptr(which initially points to the beginning of the string) by popping characters off the stack. we continue this process until the original string is exhausted.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char temp;
char arr[]= "xyz_pqr_abc";
int i=0,j=0;
while(j<11)
{
// cout << i << endl;
i=j;
cout << "i= " << i << endl;
while(arr[j]!='_' && j < 11)
{
// cout << j << endl;
j++;
cout << "j =" << j << endl;
}
j=j-1;
int k;
for(k=i;k<=(j+i)/2;k++)
{
printf("%d ",k);
temp = arr[k];
arr[k] = arr[j-k+i];
arr[j-k+i]=temp;
}
cout << endl;
j=j+2;
}
cout << arr << endl;
system("PAUSE");
return 0;
}
Here is an Iterative version:
...
char szText[]="xyz___pqr___abc";
cout << "Source text: " << szText << endl;
size_t iLen = strlen( szText );
size_t iPos = 0;
char *pszText = szText;
while( iPos < iLen )
{
if( *pszText++ != '_' )
iPos++;
else
{
strcpy( &szText[iPos], pszText );
pszText = &szText[iPos];
iLen--;
}
}
iLen = strlen( szText );
char t;
for( size_t i = 0; i < iLen; I += 3 )
{
t = szText[i];
szText[i] = szText[i+2];
szText[i+2] = t;
}
cout << "Corrected text: " << szText << endl;
...
Output:
Source text: xyz___pqr___abc
Corrected text: zyxrqpcba
- papaya October 30, 2010