Bloomberg LP Interview Question
Financial Software Developerssearch the string for comma, whenever it is found, copy the value from next character into comma.
The O(n) solution is:
#include <iostream>
using namespace std;
int main() {
int count = 0, i = 0;
char num[] = "34,00,500";
for(i=0 ; i<9; ++i)
{
if(num[i] == ',') {
count++;
continue;
}
if(count >0) {
num[i-count]=num[i];
}
}
num[i-count]='\0';
cout << "The String is : " << num << endl;
}
int nLen = strlen(str);
char* pWriteTo = str;
char* pReadFrom = str;
for (int i = 0; i < nLen+1; i++)
{
if (*pReadFrom != ',')
{
*pWriteTo = *pReadFrom;
pWriteTo++;
}
pReadFrom++;
}
this will move the null as well :)
The above won't work: (1) p is in data segment and can't be written to; (2) ignored the cumulating ','; (3) didn't end the new string properly;
Should be:
char p[] = "345,000,000";
printf("%s\n", p);
char* pReadFrom = p;
char* pWriteTo = p;
while(*pReadFrom != '\0')
{
if (*pReadFrom != ',')
*pWriteTo++ = *pReadFrom;
pReadFrom++;
}
*pWriteTo = '\0';
printf("%s\n", p);
just replace the commas with space character and you will get the same string without commas
int main()
{
string number = "345,000,000";
for(int i=0;i<number.length();i++)
{
if(number.at(i) == ',')
{
number.erase(i, 1);
number.insert(i, " ");
}
}
cout<<number<<endl;
return 0;
- char * header memory is used to create a pointer. There is no memory used to store data characters.
void Remove(char *inputParam, char delimitor) {
char *header = inputParam;
while(inputParam){
if(*inputParam != deimitor){
*header = inputParam;
header++;
}
inputParam++;
}
*header = 0;
}
int main()
{
char buf[] = "345,678,895";
printf("buf = %s\n", buf);
int ctr=0, len = strlen(buf);
for(int i=0; i<len; ++i)
{
if(buf[i]==','){
++ctr;
memmove(&buf[i], &buf[i+1], strlen(&buf[i+1]));
}
}
buf[len - ctr] = '\0';
printf("buf = %s\n", buf);
}
$ ./a.out
buf = 345,678,895
buf = 345678895
void RemoveComma(char *str){
int i=0,j=0;
while(str[i]!='\0'){
if(str[i]!=','){
str[j]=str[i];
j++;
}
i++;
}
str[j]='\0';
}
Yup, this is right. This is what I told in the interview and the interviewers seemed satisfied with the answer.
how can this be correct?? j is set to 0th location of str, so first time the ',' appears it will overwrite the first location with a ',' . the final str would just be of commas
<pre lang="c++" line="1" title="CodeMonkey65210" class="run-this">#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"enter\n";
cin>>s;
int pos,i=0;
while(1)
{
pos=s.find(',',i);
if(pos==-1)break;
// cout<<pos;
s.erase(s.begin()+pos);
i=pos;
}
cout<<s;
return 0;
}</pre><pre title="CodeMonkey65210" input="yes">3,500,000
</pre>
int main()
{
char num[] = "123,456,789";
int i = 0, commaCtr = 0;
while(num[i] != '\0')
{
if(num[i] == ',')
{
++commaCtr;
}
else
{
num[i-commaCtr] = num[i];
}
i++;
}
num[i - commaCtr] = '\0';
return 0;
}
1.The count of number of commas is required.
2.Every character must be moved left 'no of commas seen' times.
3.Works for any number of commas present anywhere inside the number.
- Dee April 12, 2010