Question

I have a fucntion which in it I want to return a string (i.e array of chars) with no spaces at all. This is my code, which in my understanding is not right:

char *ignoreSpace( char helpArr[], int length ){
    int i = 0; int j = 0;
    char withoutSpace[length];
    while ( i < length ){
        /*if not a space*/
        if ( isspace( helpArr[i] ) == FALSE )
            withoutSpace[j] = helpArr[i];
        i++;
    }
    return *withoutSpace;
}

My intention in the line:

return *withoutSpace;

Is to return the content of the array withoutSpace so I could parse a string with no spaces at all.

Can you please tell me how can I make it any better?

Was it helpful?

Solution

Your current solution will lose the result of withoutSpace when the function returns as it is only defined in that function's scope.

A better pattern would be to accept a third argument to the function which is a pointer to a char[] to write the result into - in much the same way the standard functions do, (eg strcpy.

char* ignoreSpace(char* src, char* dst, int length) {
    // copy from src to dst, ignoring spaces
    // ...
    // ...
    return dst;
}

OTHER TIPS

Try this (assuming null terminated string)

void ignoreSpace(char *str) {
   int write_pos = 0, read_pos  = 0;
   for (; str[read_pos]; ++read_pos) {
      if (!isspace(str[read_pos]) {
         str[write_pos++] = str[read_pos];
      }
   }
   str[write_pos] = 0;
}

You cannot return a pointer to a local variable from a function, because as soon as you leave the function all local variables are detroyed and no longer valid.

You must either

  1. Allocate space with malloc in your function and return a pointer to that allocated memory

  2. not return a pointer from the function butmodify directly the original string.

First solution :

char *ignoreSpace(char helpArr[], int length)
{
  int i=0; int j=0;
  char *withoutSpace = malloc(length) ;

  while(i <= length)
  {
    /*if not a space*/
    if(isspace(helpArr[i]) == FALSE)
        withoutSpace[j++] = helpArr[i];

    i++;
  }

  return withoutSpace;
}

Second solution:

char *ignoreSpace(char helpArr[], int length)
{
  int i=0; int j=0;

  while(i <= length)
  {
    /*if not a space*/
    if(isspace(helpArr[i]) == FALSE)
        helpArr[j++] = helpArr[i];

    i++;
  }

  return helpArr;
}

There are some other small correction in my code. Finding out which ones is left as an exercise to the reader.

You don't increment j, ever. In the case that the current character of the source string is not a space, you probably would like to store it in your output string and then also increment the j by one; so that you'd store the next possible character into the next slot instead of overwriting the 0th one again and again.

So change this:

...
    withoutSpace[j] = helpArr[i];
...

into this:

...
    withoutSpace[j++] = helpArr[i];
...

And then also append your withoutSpace with a 0 or '\0' (they are the same), so that any string processing function may know its end. Also return the pointer, since you should do that, not the *withoutSpace or withoutSpace[0] (they are the same):

char *ignoreSpace( char helpArr[], int length ){
    int i = 0; int j = 0;
    char * withoutSpace = malloc( length * sizeof * withoutSpace ); // <-- changed this
    while ( i < length ){
        /*if not a space*/
        if ( isspace( helpArr[i] ) == FALSE )
            withoutSpace[j++] = helpArr[i]; // <-- replaced j with j++
        i++;
    }
    withoutSpace[j] = 0;    // <-- added this
    return withoutSpace;
}

And then you should be good to go, assuming that you can have variable-length arrays.

Edit: Well, variable-length arrays or not, you better just use dynamic memory allocation by using malloc or calloc or something, because else, as per comments, you'd be returning a local pointer variable. Of course, this requires you to manually free the allocated memory in the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top