简体   繁体   中英

removing blanks from a string in c

I know its a question that been asked many times before, but i'm not asking for the solution itself, but to know why my solution isn't working.

this is my solution:

void delete_blanks(char *string)
{
    while (*string)
    {
        if (*string == ' ')
            *string = '\0';

        *string++;
    }
    puts(string);
}

The program just printing blanks (" ") for every input.
while running with the debugger, I saw that *string is pointing for '\\0' at the end. Is it possible to do it "in-place"?

This is the original solution found here:

void RemoveSpaces(char* source)
{
  char* i = source;
  char* j = source;
  while(*j != 0)
  {
    *i = *j++;
    if(*i != ' ')
      i++;
  }
  *i = 0;
}

Here is a good in-place implementation

int main(void) {
    char inStr[] = "a cat is on the moon";

    int end = 0;

    for (int i = 0; i < strlen(inStr); i++) {
        if (inStr[i] != ' ') {
            if (i != end) {
                inStr[end] = inStr[i];
            }
            end++;
        }
    }

    inStr[end] = '\0';

    printf("%s\n", inStr);
}

Probably because you are modifying the source, the parameter passed to that function, when it looks for spaces, shortcut the source to '\\0', so you are not printing the correct result. try to use other pointers like the examples you give.

char a[]="it is a sunny morning";
int j=0;
for( int i=0;i<strlen(a);i++){
    if(a[i]!=' '){
        a[j]=a[i];
        j++;
    }
}
for(int i=j;i<strlen(a);i++){
    a[i]='\0';
}
printf("%s",a);

first group the letters without space then remove the letters that exceeds the space.

output itisasunnymorning

It's unclear what you mean by "removing the blanks". If all you want to do is print the string without the spaces, you could reduce you function to this:

void delete_blanks(char *string)
{
    while (*string)
    { 
        if (*string != ' ')
             putchar(*string);
        string++;
    }
    putchar('\n');
}

If you want to remove the spaces in the underlying string it becomes a bit more complex as you would have to move characters after a ' ' to fill in the gaps. There is no 'empty character' you can insert to fill the hole in the string, you must move the remaining characters to fill it.

Basically there a 2 major things wrong with your function. You are replacing spaces with string terminating characters, signaling that the string ends here. This is not what you want to do as there might be characters after the space. In fact, if you only want to print the string you should not be modifying it at all.

When you are done iterating over the string, string points to the end of the string. Essentially passing an empty string to puts() . You need to pass the original value, before you did a lot of ++ on it, to puts()

You are also doing *string++ when you should really be doing string++. This actually works since it is parsed as *(string++) but is terrible for readability.

If you want to remove the spaces between the words of a string,
the direct answer as follows:

// #include <ctype.h>
void RemoveSpaces(char *str) {
    char *strBuff = (char*) malloc(strlen(str) + 1), *p = strBuff;
    while (*str) {
        if (!isspace(*str)) {
            *p = *str;
            p++;
        }
        str++;
    }
    *p = '\0';
    printf("%s\n", strBuff);
    free(strBuff);
}

int main(){
    RemoveSpaces("Thank you for help");
}

The Result:

Also, there is another way and gives the same result as follows:

void RemoveSpaces(char *str) {
    while (*str) {
        if (!isspace(*str)) {
            putchar(*str);
        }
        str++;
    }
    printf("%s\n", str);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM