0

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;
} 

raezino
  • 1
  • 2
  • If you literally have `\0` in your string then you have more than one string. (Or you have a string and other memory...) The `\0` is the string terminator in C. If you are reading past one -- you are a test pilot. – dawg Jan 24 '22 at 16:46
  • Sounds like what you *really* need is a way to not have the `\0` in there in the first place. – Scott Hunter Jan 24 '22 at 16:47
  • sorry my bad... – pmg Jan 24 '22 at 16:48
  • there is a scanf for k every loop – raezino Jan 24 '22 at 16:49
  • https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf – zwol Jan 24 '22 at 16:52
  • i am already using fgets for the string i believe. And the problem is with the string not the integers – raezino Jan 24 '22 at 16:54
  • Why are you inserting the null character into your string in the first place? – mzimmers Jan 24 '22 at 17:38
  • First of all, `\0` is not "whitespace". Whitespace is a catch-all term for characters that don't put any "ink" on the "paper". The normal whitespace characters are space `' '`, tab `'\t'`, and newline `'\n'`. The null character `\0` is completely different: it's an invisible marker which marks the *end* of a string, and therefore never appears *in* a string. – Steve Summit Jan 24 '22 at 17:45
  • Your problem, I think, is that you've got one place where you forgot that strings and arrays in C are 0-based. If string `s` has length `l`, the valid characters in the string run from `0` to `l-1`, and the next character "after" the string is `l`. So in the `l – Steve Summit Jan 24 '22 at 17:51
  • Another minor problem is that you're allocating 51 characters for `s`, but then telling `fgets` that the size is 21. But you're certainly not going to be able to read the string "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.". – Steve Summit Jan 24 '22 at 17:54
  • You have `scanf("%d ",&k);` — [What is the effect of trailing white space in a `scanf()` format string?](https://stackoverflow.com/q/19499060/15168) explains why that is a bad idea. – Jonathan Leffler Jan 24 '22 at 18:23

0 Answers0