Bloomberg LP Interview Question
Financial Software Developers#include <iostream>
char *rpl_mult_spaces(char *input);
int main()
{
char *result=rpl_mult_spaces("aaa bb c d");
printf("%s",result);
return 0;
}
char *rpl_mult_spaces(char *input)
{
int len=strlen(input);
char *p=input;
char* dest=new char[len];
memset(dest,'\0',len);
char *q=dest;
int i=0;
while(i<len)
{
if(*p!=' ')
{
*q=*p;
p++;
q++;
i++;
}
else
{
while(*p==' ')
{
p++;
i++;
}
*q=' ';
q++;
}
}
return dest;
}
#include <stdio.h>
#include <stdlib.h>
int main(void){
char a[100] ;
int i , j ;
i = j = 0 ;
printf("Enter the string\n") ;
fgets(a,sizeof(a),stdin);
a[strlen(a) -1 ] = '\0' ;
while(a[i] != '\0'){
while(a[i] != ' ' && a[i] != '\0'){
a[j]=a[i] ;
i++ ;j++;
}
a[j]=a[i] ;j++ ;
while(a[i] == ' ' && a[i] != '\0') i++ ;
}
printf("New string:%s\n",a) ;
}
void remove_extra_space(char* str){
char *removedPtr = str;
while(*str){
if(*str == ' ')
*removedPtr++ = *str++;
while(*str == ' ')
++str;
*removedPtr++ = *str++;
}
*removedPtr = '\0';
}
btw: if you guys could put the code inside
, it makes reading much easier
void spacing(char* str)
- Sush November 27, 2008{
int read, write;
read = write = 0;
while(str[read])
{
//copy till a white space has been found
while(str[read] != ' ' && str[read])
{
str[write++] = str[read++];
}
//on a whitespace
if(str[read] == ' ')
{
str[write++] = str[read++];
}
//keep incrementing read till no more white spaces are found
while(str[read] == ' ' && str[read])
{
read++;
}
}
//Null terminate
str[write] = '\0';
}