Bloomberg LP Interview Question
Financial Software Developerschar* removeExtraSpaces(const char* str) {
const char* SPACE = " ";
int len = strlen(str) + 1;
char* buf = new char[len];
char* s = new char[len];
char* t = NULL;
strcpy(buf, str);
t = strtok(buf, SPACE);
if (strcmp(str, t) == 0) return t;
do strcat(s, t); while ((t = strtok(NULL, SPACE)) && strcat(s, SPACE));
delete[] buf;
return s;
}
char* removeExtraSpaces(const char* str) {
if (str == NULL) return NULL;
int len = strlen(str) + 1;
char* buf = new char[len];
strcpy(buf, str);
if (len == 1) return buf;
const char* SPACE = " ";
char* s = new char[len];
char* t = NULL;
t = strtok(buf, SPACE);
if (strcmp(str, t) == 0) return t;
do strcat(s, t); while ((t = strtok(NULL, SPACE)) && strcat(s, SPACE));
delete[] buf;
return s;
}
void removeSpace(char* s){
int hop_forward_steps = 0;
while(*s != '\0'){
s++;
if(*s == ' ' && *(s-1) == ' '){
hop_forward_steps++;
continue;
}
//jump forward by hop_forward_steps
*(s-hop_forward_steps)=*s;
}
*(s-hop_forward_steps) = '\0';
}
bool removeExtraSpace(char c)
{
static bool isPrevSpace = true;
bool retCode = false;
if ((c == ' ') && isPrevSpace)
{
retCode = true;
}
else
{
retCode = false;
}
if (c == ' ')
{
isPrevSpace = true;
}
else
{
isPrevSpace = false;;
}
return retCode;
}
//class Test
int _tmain(int argc, _TCHAR* argv[])
{
std::string realStr;
std::getline(std::cin,realStr);
realStr.erase(std::remove_if(realStr.begin(),realStr.end),removeExtraSpace),realStr.end());
return 0;
}
string removeExtraSpaces (const string instr) {
string ret;
string::size_type nonspace = instr.find_first_not_of (" ");
string::size_type nextDoubleSpace;
while (nonspace != instr.npos) {
nextDoubleSpace = instr.find (" ", nonspace);
if (nextDoubleSpace == instr.npos) {
ret.append (instr.begin() + nonspace, instr.end());
} else {
ret.append (instr.begin() + nonspace, instr.begin() + nextDoubleSpace+1);
}
nonspace = instr.find_first_not_of (" ", nextDoubleSpace);
}
return ret;
}
char* remove_spaces(char* str)
{
char* ptr1 = str;
int i = 0;
char* new_str = (char*)malloc(sizeof(str));
if (new_str==NULL)
exit(1);
bool start = false;
char* tmp = new_str;
while(!start)
{
if(*ptr1 == 32)
{
ptr1++;
continue;
}
start = true;
strncpy(tmp,ptr1,1);
tmp++;
ptr1++;
}
while(*(ptr1 + 1) != '\0')
{
if(*ptr1 == 32 && *(ptr1 + 1) == 32)
{
ptr1++;
continue;
}
strncpy(tmp,ptr1,1);
tmp++;
ptr1++;
}
*tmp = '\0';
return new_str;
}
void remove_spaces(char* str)
{
if(str==NULL) return;
int pos =0;
bool firstblank;
char * current=str;
while(current)
{
if(*current!=''))
break;
current++
}
while(current)
{
if(*current!='')
{
str[pos++]=*current;
firstblank = true;
}
else
{
if(firstblank)
{
str[pos++]=*current;
firstblank = false;
}
}
current++;
}
str[pos]=0;
return;
}
void remove_spaces(char* s)
{
int len = strlen(s);
char* dest = (char*) malloc(len+1);
*dest = '\0';
char* delimiter = " ";
char* p = strtok(s,delimiter);
if( p != NULL )
{
do
{
strcat(dest,p);
strcat(dest," ");
}while( p = strtok(NULL, delimiter) );
}
//remove the last space
dest[strlen(dest)-1] = '\0';
strcpy(s,dest);
}
only using pointer manipulations:
#include <stdio.h>
#include <assert.h>
char* remove_extra_space(char* stringIn)
{
assert(*stringIn != '\0');
char *p = stringIn;
char *q = p;
int newSpace=1;
while( *q != '\0') {
if(*q == ' ' && newSpace == 1) {
*p=*q; p++; q++; newSpace=0;
}
else if(*q == ' ' && newSpace == 0){
q++;
}
else if(p!=q) {
*p = *q; p++; q++; newSpace=1;
}
else {
p++; q++; newSpace=1;
}
}
*p = *q;
return stringIn;
}
int main() {
char a[]=" sdfaiyher ius dfa ere ";
//char a[]=" ";
printf("old string=%sEND\n",a);
printf("new string=%sEND\n",remove_extra_space(a));
}
Can someone please test this rigorously. I have tested it as much as i can and it works fine
public void removespaces(String s){
char[] str = s.toCharArray();
String tgt = "";
int p1 = 0 , trgt = 0;
while(p1<s.length()){
if(str[p1]==' '){
tgt+=str[p1];
while(str[p1]==' '){
p1++;
if(p1== s.length())break;
}
}
if(p1== s.length())break;
tgt+=str[p1];
p1++;
}
System.out.println(tgt);
}
char* removeExtraWhiteSpaces(const char* str)
{
int len = strlen(str);
char* return_str = new char[len+1];
if(len <= 1)
return strcpy(return_str, str);
int count = 0;
char next, current = *str;
while(*str != '\0')
{
next = *(str+1);
if (! (current == ' ' && next == ' ') )
{
return_str[count++] = current;
current = next;
}
++str;
}
return_str[count] = '\0';
return return_str;
}
// Repost with indentation
char* removeExtraWhiteSpaces(const char* str)
{
int len = strlen(str);
char* return_str = new char[len+1];
if(len <= 1)
return strcpy(return_str, str);
int count = 0;
char next, current = *str;
while(*str != '\0')
{
next = *(str+1);
if (! (current == ' ' && next == ' ') )
{
return_str[count++] = current;
current = next;
}
++str;
}
return_str[count] = '\0';
return return_str;
}
const char * removeExtraSpaces(char * str)
{
if (!str) {
return str;
}
if(strlen(str)==1)
{
return str;
}
int read=1;
int write=0;
while (str[read]!='\0') {
if (str[read]==' '&&str[write]==' ') {
read++;
}
else {
write++;
str[write]=str[read];
read++;
}
}
str[++write]='\0';
return str;
}
Time complexity: O(n). I am not sure if this is a solution
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
int main () {
char *s = " This is an example ";
char r[100];
int i = 0;
printf("%s\n",'\0');
while (*s){ //!= '\0') {
if (*s != ' ') {
r[i] = *s;
i++;
}
else if(i > 0&& r[i-1] != ' '){
r[i] = ' ';
while((*(++s)) && (*s) == ' '){}
i = (*s)? i+1 : i;
s--;
//i++;
}
s++;
}
r[i] = '\0';
printf ("Result:%s", r);
printf("good\n");
}
- Anonymous July 26, 2009