hello im new to programming and i have a question. I made the program below for my programming classes and i have a problem when i submit the answer. The results are visibly correct but when i check the whitespace characters output this happens: (what i need is conclusion is a way to remove '\0' from the middle of the pointer string)
input :
3
7 Hello World
20 Hello World
30 The story, told by the sixyear-old Jean Louise Finch, takes place during three years (1933–35) of the Great Depression in the fictional town of Maycomb, Alabama, the seat of Maycomb County.
the expected output (with whitespace characters) :
Hello W\n
Hello World*********\n
The story, told by t**********\n
\n
my output ( whitespace characters) :
Hello W\n
Hello World\0*********\n
The story, told by t\0**********\n
\n
and my source code is :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *s;
int n,i,k,l,j,o,p;
s = malloc(51*(sizeof(char)));
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d ",&k);
fgets(s,21,stdin);
s[strcspn(s, "\n")] = '\0';
l = strlen(s);
if (l==k)
{
printf("%s\n",s);
}
else if (l>k)
{
for (j=0;j<k;j++)
{
printf("%c",s[j]);
}
printf("\n");
}
else if (l<k)
{
s = realloc(s,(k+1)*sizeof(char));
for (o=l+1;o<=k;o++)
{
s[o]='*';
}
s[k+1]='\0'
for (p=0;p<=k;p++)
{
printf("%c",s[p]);
}
printf("\n");
}
}
return 0;
}