Home > Software design >  c remove space function stops when space is hit
c remove space function stops when space is hit

Time:11-11

I'm creating a function in c where if there is a space in a char array, it will remove it and print the array. The problem i'm having is that when I hit a space, the function stops copying the rest of the array to the final string.

this is the code:

void removeSpace(char *str) {
    char *temp = malloc(1000 * sizeof(char));
    strcpy(temp, str);
    str = malloc(1000 * sizeof(char));

    for (int i = 0; i < strlen(temp); i  ) {
        while (temp[i] == ' ') {
            i  ;
        }
        str[i] = temp[i];
        printf("str i: %c, temp i: %c\n", str[i], temp[i]);
    }
    printf("str: %s\n", str);

}

And this is inside the "main" function:

int main(main) {
    char *test = malloc(1000 * sizeof(char));
    strcpy(test, "hello world");
    removeSpace(test);

The result I get is this:

str i: h, temp i: h
str i: e, temp i: e
str i: l, temp i: l
str i: l, temp i: l
str i: o, temp i: o
str i: w, temp i: w
str i: o, temp i: o
str i: r, temp i: r
str i: l, temp i: l
str i: d, temp i: d
str: hello

If anyone could help I would appreciate it!!!

CodePudding user response:

You've got a few issues here. Some minor ones first.

The first thing you do is malloc some memory, but don't free it, so it'll leak. As a side note, you don't need to copy the str parameter at the start, since you're making a copy of it anyway.

I'll assume this is just an exercise, so the fact that you're just making a guess that the string is less than 1000 characters doesn't matter, but even so, you could take the length of first to allocate the right size of memory.

Because of the way C strings work, strlen() finds the length by scanning from the beginning until it finds a '\0' character, so for (...; i < strlen(temp); ...) will scan the whole string every time through the loop. The source length doesn't change, so save it in a const variable and compare to that in the loop.

The root of the problem is that you're using the same index i for str as for temp. When you skip characters in temp, you want to use a different index for str which is not incremented. Something like this might work (I haven't tested it):

void removeSpace(char *str) {
    const size_t str_len = strlen(str);
    const size_t str_size = str_len   1; // include '\0'
    char *temp = malloc(str_size * sizeof(char));

    // Copy str to temp but skip ' '.
    int temp_idx = 0;
    for (int i = 0; i < str_size; i  ) {
        while (str[i] == ' ') {
            i  ;
        }
        temp[temp_idx] = str[i];
        temp_idx  ;
        printf("temp i: %c, str i: %c\n", str[i], temp[i]);
    }
    printf("temp: %s\n", temp);
    free(temp);
}

CodePudding user response:

it will remove it and print the array.

Alternative: If OK to change the input string

Walk the string and copy the non-string portion back on itself.

void filter_out_spaces_and_print(char *src) {
  char *origin = src;
  char *dest = src;
  while (*src) {
    if (*src != ' ') {
      *dest   = *src;
    }
    src  ;
  }
  *dest = '\0';
  printf("%s", origin);
}
  • Related