0

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

3
  • what are the shortcomings of the current code you have posted? Commented Mar 31, 2011 at 14:35
  • Its didnt work. str[strlen(i)] = '\0'; will be str[strlen(str)] = '\0'; Commented Mar 31, 2011 at 14:37
  • homework? no problem if it is, just mark it with the tag so you get better answers Commented Mar 31, 2011 at 14:50

5 Answers 5

3

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

1 Comment

Yes, the strings created by gets are 0 (null) terminated when they haven't overflowed the buffer you give it.
1

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:

    if (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:

    
    str[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.

Comments

0

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

Comments

0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.