Microsoft Interview Question


Country: -




Comment hidden because of low score. Click to expand.
0
of 0 vote

i think this should work...
here array 'a' is input unformatted string and array 'b' is formatted string. '@' has been used as delimiter to stop taking input.

#include<stdio.h>
#include<string.h>

char b[100];
void formatter(char a[]);
int main()
{
char a[100];
int i=0;
while(a[i-1]!='@')
scanf("%c",&a[i++]);
a[i-1]='\0';
printf("\n len is %d",strlen(a));
printf("\n \n %s",a);
formatter(a);
printf("\n%s\n",b);
return 0;
}

void formatter(char a[])
{
int k=0,i=0;
for(;a[i]!='\0';i++)
{
if(a[i]=='\n'&&a[i-1]!='.')
{
b[k++]=' ';
continue;
}
if(a[i-1]=='.'&&a[i]!='\n')
b[k++]='\n';
b[k++]=a[i];
}
}


sample input :

this is
unformatted string.output gives
the proper formatted string.

program output :

this is unformatted string.
output gives the proper formatted string.

- kim October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would think that there are other marks that can set off a sentence, such as ? and !. Also, not every . delimits a sentence, so perhaps I would ask my interviewer about that.

ex. So...what are you doing today?

- eugene.yarovoi October 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If I understand correctly, the length of the output string would increase. For example, if the input string is "I like Dogs. But I do not like Cat." has a length of 34. If you format the string the output would be
"I like Dogs.
But I do not like Cat."

You are basically adding a '\n' to the string, where the number of '\n' or CR is equivalent to the number of periods. So, the output string would be of size 35 in this case. Your solution is assuming same size for input and output string, which is incorrect.

- Anonymous October 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there was a typo above in my comment. The output string would be of length 36.

- Anonymous October 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i would appreciate if you would tell me where , in the problem , it is specified that we can't increase the length of the string.

- kim October 11, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More