Thread: Remove space in a string

  1. #16
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    writting it in assembly might speed it up but looping through yourself can makeup for it on shorter strings simply because you don't have the overhead of a function call.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not to mention the fact that modern compilers are pretty handy at optimizing a good deal of the time.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    of course you could use sizeof too.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonpuz
    of course you could use sizeof too.
    No you can't. You can only use sizeof on an object that has been created in scope. Thus:
    Code:
    void foo( char * bar )
    {
        char foobar[BUFSIZ];
        int x = sizeof( foobar ); /* valid */
        int y = sizeof( bar ); /* invalid */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I see, thats two new things i learned today :P

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you can use sizeof bar but it will just give you the size of a pointer to a character

  7. #22
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    I am glad to see that many expert replying my question,
    Can someone explain the code from Kip,

    Code:
    while (isspace((unsigned char)*end)) {
        --end;
      }
      *++end = '\0';
      p = mem;
      while (isspace((unsigned char)*p)) {
        ++p;
      }
    Thank a lot.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while (isspace((unsigned char)*end)) {
        --end;
      }
      *++end = '\0';
      p = mem;
      while (isspace((unsigned char)*p)) {
        ++p;
      }
    Well there are a few things:
    1) There is no need to typecast this pointer. And if you're going to do so, then it should be done correctly. isspace expects an int, not an unsigned char. It's doubly wrong do use an unsigned char, because in doing so, you're changing the char to something else, because the sign bit is being treated differently than it normally would. It's best to leave it alone, and let it be auto-converted to an int when it's passed.

    2) The end pointer is set initially to the end of the string. Then, as long as it's a white space character, the pointer is decremented until it reaches a character that is no longer white space. Once that character is reached, the pointer is incremented to the space beyond it, to the last white space character. It then is set to null, as to terminate the string.

    3) The p pointer is set to the beginning of the string, and the same thing is done there, only advancing from the front, instead of from the back. However, this time you don't do anything to the string, you're just ignoring all preceeding white space.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by quzah
    1) There is no need to typecast this pointer. And if you're going to do so, then it should be done correctly. isspace expects an int, not an unsigned char. It's doubly wrong do use an unsigned char, because in doing so, you're changing the char to something else, because the sign bit is being treated differently than it normally would. It's best to leave it alone, and let it be auto-converted to an int when it's passed.
    The question isn't about the type. A value cast to unsigned char will then be converted to int. The question is about the range of values involved. All character mapping functions declared in <ctype.h> accept an integer argument, but the value of that int must be representable by unsigned char, or equal EOF. Anything else is undefined. The cast to unsigned char forces the value of a character into the correct range on systems where char is signed.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The question isn't about the type.
    Of coruse it's about type. Typecasting a char to an unsigned char is the wrong type. You're unsigning a value that is signed. That's wrong. That's what I said was wrong. What happens is what I said happens:

    1)The signed char is typecast to an unsigned value. This screws with the signed bit, changing the value.
    2) The new value is then auto cast to an int.

    The cast to unsigned char forces the value of a character into the correct range on systems where char is signed.
    No. That's wrong. Forcing the character to unsigned does not change "correct" the value. There is [b]never[/color] a reason to typecast to an unsigned character for these functions. If it is a char, it will auto promote to an integer. That's normal. That's what it's meant to do. Making it unsigned just screws it up.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I haven't been following too closely, but I believe this is the argument [edit](in favor of the cast)[/edit].
    Last edited by Dave_Sinkula; 05-06-2004 at 07:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not a very good argument. For starters, EOF cannot be mapped to an unsigned value, because it is a negative number so I fail to see the purpose of the mapping. It's already determined that the is* functions will:
    "C: A Reference Manual, 5th Edition"

    All of the facilities described here operate properly on all values representable as type char or type unsigned char, and also on the value of EOF, but are undefined for all other integer values unless the indivudual description states otherwise.
    So again, there is no purpose for the cast. It does nothing benificial, especially since the function takes an int as an argument anyway. The point would be, that were you to have a signed value for type char, and you cast it to unsigned char, it would distort the passed value from what it actually was.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Registered User
    Join Date
    May 2004
    Posts
    127
    "C: A Reference Manual, 5th Edition"

    All of the facilities described here operate properly on all values representable as type char or type unsigned char, and also on the value of EOF, but are undefined for all other integer values unless the indivudual description states otherwise.
    Your reference manual is wrong. It always has been on several subtle points since the first edition. The current C standard says this.
    7.4 Character handling <ctype.h>

    1 The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.
    There is no mention of plain char, only a strict requirement that the value be representable by unsigned char (making other values of signed char undefined) or EOF. If you pass a plain char to one of the <ctype.h> functions and the value is negative and not EOF, the conversion to int will preserve the sign and invoke undefined behavior. On the other hand, if you cast the argument to unsigned char, the value will be forced into the correct range and will remain thus when converted to int.

    If the value is already in the correct range, such as with the return value of getchar, then the cast is not necessary. But for characters from a string that you can't be sure about without testing, the cast is a very good idea.

    Feel free to ask anyone you want and perform any tests you want. Though for confirmation I would recommend comp.std.c as there should be no doubt about the experts that reside there.

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, considering that the only required supported characters are the 128 ASCII characters, it wouldn't matter if it was signed, since they all fall in the first seven bits of a signed char implementation.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    May 2004
    Posts
    1

    Hi, a small solution, quick and dirty.

    This is a simple, quick and dirty solution, but it works. It replaces any spaces greater than one with a single place, so I'm sure you could implement an array and delete all spaces that are at the beginning and end of a string with ease.

    Here's the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int c, copy;
    
    	while((c = getchar()) != EOF){
    		if(c == ' '&& copy == ' ') continue;
    		else putchar(c);
    		copy = c;
    	}
    	return 0;
    }
    So, as I was saying, by throwing in an array, and throwing in another loop (up to you) and swapping out the use of putchar, you should be able to easily accomplish this.

    Not necessarily the best way to do it, but I haven't slept in a while. :P

    Oh, forgot to mention that the EOF in this program (in windows xp) is read as CTRL+Z - so if you were wondering how to exit, that's how.
    Last edited by Drevay; 05-07-2004 at 07:17 PM. Reason: Forgot to add something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM