Suppose you are passing a string to a Formatter function ...
Microsoft Interview Questions and Answers
(Continued from previous question...)
19. Suppose you are passing a string to a Formatter function ...
Question
Suppose you are passing a string to a Formatter function. Get the formatted news feed output string such that* There should be one sentence per line.* There shouldn't be any spaces i.e. the line shouldn't be blank.
maybe an answer:
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.
(Continued on next question...)
Other Interview Questions
|