Bloomberg LP Interview Question
Financial Software Developersi do agree that logic is simpler here than 'esoteric' bodyless loops above :)
One difference between what i hve above vs. this is that if there are no spaces in the string, above code does no copying, whereas as this one copies each character onto itself. Though not a big deal, it can easily fixed by adding a loop ahead to place i at the first space.
Question is kinda unclear....But from what I understand, the idea is to move the spaces to the end of the string, inplace and then copy all the valid input to a new string and return that. Delete the old string. I believe this is his idea of inplace...
Idea would be to find first space (using p), and from then on move every non space chacacter to the writable position, which is p,p+1,p+2,...
void removeSpaces(char *p) {
assert(p);
while (*p && *p++ != ' ' ); // move p just past first space
char *q = p; // p,q both point past first space
while ( --p, *--q ) { // q points to first space, p to next writable pos
while (*++q == ' ' ); // move q to next non-space
while (*q && (*p++ = *q++) != ' '); // copy, moving q past next space
}
}
Scratch the above...
Heres a bug free one :)
void removeSpaces(char *p, char blank) {
assert(p);
// following 2 lines position p to first space or null
while (*p++ != blank && *(p-1));
p--;
char *q=p;
while ( *q ) {
// position q to next non-space
while (*q && *++q == blank);
// copy q to p, until null gets copied or while q is
// non-space
while ( *q != blank && (*p++ = *q++) );
q--; // needed to ensure q is in array bound
}
}
void RemoveSpaces(char* str)
{
if(!str) return;
int read = 0, write = 0;
//find first space character
while(str[write] != ' ')
{
if(str[++write]) continue; //there is a space
return; //there are no more space characters
}
read = write; //there were no spaces so far so read here onwards
do{
//find first non-space character
while(str[read] == ' ')
{
if(str[++read]) continue; //there is a non-space character
str[write] = '\0';
std::cout << "\n Modified String is: " << str << "\n";
return; //there are no more non-space character
}
str[write++] = str[read];
str[read++] = ' ';
//find first space character
while(str[write] != ' ')
{
if(str[++write]) continue; //there is a space
return; //there are no more space characters
}
}while(str[read]);
str[write] = '\0';
std::cout << "\n Modified String is: " << str << "\n";
}
#include <stdio.h>
void main()
{
char *s = "h e l l o";
int i = 0, j = 0;
while((s[i] != '\0') && (s[i] != ' '))
{
i++;
j++;
}
while(s[j] != '\0')
{
while(s[j] != '\0' && s[j] == ' ')
j++;
while(s[j] != '\0' && s[j] != ' ')
{
s[i++] = s[j++];
}
}
*(s+i) = '\0';
printf("%s\n", s);
}
working code
#include <iostream>
using std::cout;
using std::endl;
void removeSpace(char * str)
{
char* current = str;
if(str == NULL)
return;
if(*str == ' ')
{
//skip all the empty spaces
do
{
str++;;
}while(*str==' ');
}
while(*str != '\0')
{
if(*str==' ')
{
*current = ' ';
current++;
//skip all the empty spaces
do
{
str++;
}while(*str==' ');
}
else
{
*current = *str;
current++;
str++;
}
}
//check to see if the last characater was space
if(*(current-1) == ' ')
*(current-1) = '\0';
else
{
*current = '\0';
}
}
int main()
{
char str1[20] = " hello world !!! ";
char str2[30] = "hello world !!!";
char str3[30];
removeSpace(str1);
std::cout << str1<<std::endl;
removeSpace(str2);
std::cout << str2<<std::endl;
std::cin.getline(str3, 30, '\n');
removeSpace(str3);
std::cout << str3<<std::endl;
getchar();
}
I'm not sure why other solutions here are so ... complicated.
A simple solution in C
char* removeSpaces(char[] str)
{
if (str)
{
char* pWrite = str;
for (char* pRead = str; *pRead; pRead++)
if (*pRead!=' ')
*(pWrite++) = *pRead;
*pWrite = 0;
}
return str;
}
A solution in D
dchar[] removeSpaces(dchar[] str)
{
auto write = str;
foreach(c; str)
if (c!=' ')
{
write.front = c;
write.popFront;
}
return str[0..$-write.length];
}
If the interviewer generalizes the question, more generic solutions might be helpful.
Here again in D
E[] removeItem(E)(E[] r, E itemToRemove)
{
auto write = r;
foreach(item; r)
if (item!=itemToRemove)
{
write.front = c;
write.popFront;
}
return r[0..$-write.length];
}
E[] remove(alias predicate, E)(E[] r)
{
auto write = r;
foreach(item; r)
if (predicate(item))
{
write.front = c;
write.popFront;
}
return r[0..$-write.length];
}
Then, removeSpaces can be implemented simply as
dchar[] removeSpaces(dchar[] r)
{
return removeItem(r, ' ');
// or
return remove!(x => x!=' ')(r);
}
But I think that might be beyond the scope of the question ;-)
i=0,j=0
- Anonymous November 11, 2008while(s[j])
{
if(s[j]!=' ')
s[i++]=s[j];
j++;
}
s[i]='\0';