0

I have written a program in C and it is not outputting how I expected. Im used to linux and so I am confused why for this basic program the output is not as I expected. I am using c std 99. I am also relativley new to C so if it is a issue in my code also please let me know

for input: A B C D E F G H I J K L M N O

I get output: -A-- --B-- --C-- --D-- --E-- --F-- --G-- --H-- --I-- --J-- --K-- --L-- --M-- --N-- --O-- -

expected output -A--B--C--D--E--F--G--H--I--J--K--L--M--N--O_

int main() {
  char *arr = safeMalloc(sizeof(char) * 32);
  int sizeArr = 32;
  int cou = 0;
  while (scanf("%c", &arr[cou]) != EOF || cou == 10) {
    cou++;
    if(cou == sizeArr-2) {
      arr = realloc(arr, 2*sizeArr);
      sizeArr*2;
    }
  }
  for (int i = 0; i < cou; ++i) {
    printf("-%c-", arr[i]);
  }
  printf("\n");
}
MaxVK
  • 172
  • 2
  • 12

1 Answers1

1

The program is considering the spaces as input characters. Changing the for loop to this should fix it:

for (int i = 0; i < cou; ++i) {
    if(arr[i] == ' ') continue;
    printf("-%c-", arr[i]);
}
cs1349459
  • 911
  • 9
  • 27