简体   繁体   中英

Delete whitespace from string

I trying make program that delete whitespace from the string. Someone can give me hand?

int main()
{
    char str[15];
    int i=0;
    gets(str);
    while(str[i] !=0){
        if(str[i] == 32)
        {
            str[i++] = str[i];
            str[i] = str[i++];
        }
    }
    str[strlen(i)] = '\0';
    printf("%s", str);
    return 0;
}

thx

Well since you are using C trim is not an option. First of all, what are your thoughts on the line: str[i] = str[i++]; ? This is basically the same as i++;

Now to the code:

 char *mystring, *read, *write;
 // mystring gehts filled with a null terminated string here
 read = mystring;
 write = mystring;
 while(*read != '\0') {
      if(*read == ' ') 
           read++;
      else 
           *write++ = *read++;
 }
 *write = '\0';

If you like to write to another String, just make write point to the location where you want it to be. Do not forget to check that you have enough room there ;)

you can use 2 pointers. I'm not familiar with C, so I suppose checking str[i] ==0 means the end of a string.

int main()
{
    char str[15];
    int i=0;
    int j=0;
    gets(str);
    while(str[i] !=0 && str[j] !=0){
        while(str[j] == 32)
        {
            j++;
        }
        if ( str[j] ==0){
            break;
        }
        str[i] = str[j];
        i++;
    }
    str[strlen(i)] = '\0';
    printf("%s", str);
    return 0;
}

Several problems:

  1. NEVER NEVER NEVER NEVER NEVER NEVER use gets . First of all, it's deprecated as of C99, and will be gone from the next revision of the standard. Secondly, it will introduce a point of failure in your code (not may introduce, will introduce). Instead of gets(str) , use fgets(str, sizeof str, stdin) instead.

  2. Use character constants instead of numeric values, because all the world is not ASCII:

    \n\nif (str[i] == ' ') // not str[i] == 32 

  3. Whitespace includes tabs, form feeds, returns, etc., not just spaces; instead of checking against individual values, use the isspace() library function.

  4. The following lines are a problem:

    \nstr[i++] = str[i]; str[i] = str[i++];\nstr[i++] = str[i]; str[i] = str[i++]; 
    First of all, this should invoke undefined behavior; you're attempting to read and update an object more than once between sequence points. Second, I'm not sure of the logic. It looks like you're trying to swap elements, instead of just skipping over one.

  5. You're passing an integer value to strlen : that's not going to work. It's going to interpret that integer value as an address (you should at least be getting a warning), with potentially bad results.

You can do this in a single loop with two array indices:

#include <stdio.h>
#include <ctype.h>

int main(void)
{
  char str[15];

  if (fgets(str, sizeof str, stdin) != NULL)
  {
    size_t r, w;

    printf("Original: \"%s\"\n", str);

    /**
     * str[r] is the element that we are reading; str[w]
     * is the element that we are writing
     */
    for (r = 0, w = 0; str[r] != 0; r++)
    {
      if (!isspace(str[r]))   // if str[r] is not a whitespace character
        str[w++] = str[r];    // write it to str[w]; note that we only
                              // advance w for non-whitespace chars
    }
    str[w] = 0; // add the 0 terminator at the end

    printf("Stripped: \"%s\"\n", str);
  }
  return 0;
}

Obviously, this alters the original string. If you need to preserve the original input for some reason, you'll have to declare a second buffer to hold the modified string.

What do you intend to happen in this conditional?

  if(str[i] == 32)
        {
            str[i++] = str[i];
            str[i] = str[i++];
        }

What does actually happen? (hint: reread the section on i++)

-- pete

This code is fast and works fine.. hopes this helps for ur case.. Copy and paste into a file and test it..

#include <stdio.h>
main()
{
    char str[100];
    int len = 0;
    gets(str);
    len = strlen(str);
    int i = 0;
    while(str[i]!='\0')
    {
        if(str[i] == ' ')
        {
            memcpy(&str[i], &str[i+1], len - i);
            len--;
        }
        i++;
    }
    str[len] = '\0';
    printf("O/P : %s\n",str);
}

This works fine..

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