Microsoft Interview Question
Country: -
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.
i think this should work...
- kim October 09, 2011here 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.