Home > Back-end >  C program to remove all spaces from a given string prints some extra text despite exiting loop
C program to remove all spaces from a given string prints some extra text despite exiting loop

Time:06-11

I have written a program to remove all spaces from a given string, and have used a loop to iterate over the string to move over the spaces and add the text in a new string. The code does this but also prints some extra text (last few chars from the original string) despite it not being in a loop and not being added to the new string.

#include <stdio.h>
#include <string.h>

int main(){
    char str[] = "   My name is Harry   123 "; //Initial string
    char newStr[] = "";    //New string with no spaces
    for(int i = 0, j = 0;i<strlen(str);i  ){   //running a loop with i and j to check 
                                                and move over spaces and only add text 
                                                 in newStr
        if(str[i] == ' '){
            continue;
        }
        else{
            newStr[j] = str[i];
            j  ;
        }
    }
    printf("%s",newStr); //Printing the newStr
    return 0;
}

Here is the output:

MynameisHarry123arry   123

I also want to point out that my program runs fine if I set a size to the newStr or if I use "i<=strlen(str)" instead of "i<strlen(str)".

CodePudding user response:

I think it is because strings in c the last byte they store is '\0' that indicates the end of the string.

for "Harry" it would be like "Harry\0"

the problem is that strlen() only gives the number of characters before the '\0'. You are printing out garbage because the string you built wont have the '\0' character at the end. But with <= you read in that null character too which does not equal ' ' and puts it into you newStr[].

I hope that this will be helpful to you!

CodePudding user response:

First you need to dynamically allocate in memory newStr while building it or give it a big enough size at the initialization, because right now it's only 1 byte long ('\0') and any attempt to write more than that will throw a valgrind invalid write error that could segfault.

Then when you've finished copying it you must put at the end a '\0' to prevent any attempt of reading it to overshoot (like printf might do).

Example with reallocation :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
  char str[] = "   My name is Harry   123 ";
  char *newStr = NULL;
  int j = 0;

  for (int i = 0, len = strlen(str); i < len; i  ) {
    if (str[i] != ' ') {
      newStr = (char *)realloc(newStr, sizeof(char) * (j   2)); // new char and '\0'                             
      newStr[j] = str[i];
      j  ;
    }
  }
  newStr[j] = '\0';

  printf("%s\n", newStr);
  free(newStr);

  return 0;
}

Example with pre-allocation :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
  char str[] = "   My name is Harry   123 ";
  char *newStr = NULL;
  int lenNewStr = 0;
  int j = 0;

  for (int i = 0; str[i] != '\0'; i  )
    if (str[i] != ' ') lenNewStr  ;
  newStr = (char *)malloc(sizeof(char) * (lenNewStr   1));

  for (int i = 0, len = strlen(str); i < len; i  ) {
    if (str[i] != ' ') {
      newStr[j] = str[i];
      j  ;
    }
  }
  newStr[j] = '\0';

  printf("%s\n", newStr);
  free(newStr);

  return 0;
}
  • Related