Right trim string function in C 
Author Message
 Right trim string function in C

Hello C programmers,
        I was wondering does anybody knows how or is there
a right trim string function available in C?    

        Eg. Suppose I have the following:

           char *str = "Hello Dolly     \0"

        Is there a right trim function that will remove the
trailing spaces and make *str looks like "Hello Dolly\0"?

Thanks,
Al



Sun, 27 Oct 1996 17:59:57 GMT  
 Right trim string function in C

Quote:

> Hello C programmers,
>    I was wondering does anybody knows how or is there
> a right trim string function available in C?      

Here's one that I wrote for Publib.  Remove the include for
"publib/strutil.h".

/*
 * strrtrim.c -- remove trailing whitespace from a string
 *
 * Part of publib.  See man page for more information

 */

#include <assert.h>
#include <ctype.h>
#include <string.h>

#include "publib/strutil.h"

char *strrtrim(char *s) {
        char *t;

        assert(s != NULL);
        t = strchr(s, '\0');
        while (t > s && isspace(t[-1]))
                --t;
        *t = '\0';
        return s;

Quote:
}

--

My name is Ozymandias, king of kings/Look on my works, ye Mighty, and despair!


Sun, 27 Oct 1996 18:23:10 GMT  
 Right trim string function in C

Quote:
>    Eg. Suppose I have the following:

>           char *str = "Hello Dolly     \0"

>        Is there a right trim function that will remove the
>trailing spaces and make *str looks like "Hello Dolly\0"?

No, there isn't, but it's trivial to write it.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 27 Oct 1996 19:26:55 GMT  
 Right trim string function in C

Quote:
>    Eg. Suppose I have the following:
>           char *str = "Hello Dolly     \0"

There is no need for the \0 at the end of your string - anything in
quotes is a string constant, which is automatically NUL terminated for
you by the compiler.  (The exception is string constants that are
begin concatenated: char *foo = "hi" " there";, where the NUL is only
placed at the end of the concatenated string).

--
#include        <standard.disclaimer>
 _
Kevin D Quitt 91351-4454                96.37% of all statistics are made up



Mon, 28 Oct 1996 04:34:52 GMT  
 Right trim string function in C

Quote:
>    I was wondering does anybody knows how or is there
>a right trim string function available in C?        

>    Eg. Suppose I have the following:

>           char *str = "Hello Dolly     \0"

>        Is there a right trim function that will remove the
>trailing spaces and make *str looks like "Hello Dolly\0"?

The standard C run time libraries do not have one. Here is one that
works ... this better not be solving your homework assignment. Since
it is pretty late in the semester, I will make this assumption.

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

void
trim_trailing ( char *s )
{
    char *c;

    if ( s != (char *) NULL )
    {
        c = s + strlen ( s ) - 1;
        while ( c >= s && isspace ( *c ) )
            --c;
        *++c = '\0';
    }

Quote:
}

--
-------------------------------------------------------------------------
| Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |

-------------------------------------------------------------------------


Tue, 29 Oct 1996 15:01:15 GMT  
 Right trim string function in C

Quote:

>The standard C run time libraries do not have one. Here is one that
>works ... this better not be solving your homework assignment. Since
>it is pretty late in the semester, I will make this assumption.

Here's another run at the problem.  It trims whitespace from both ends
and optionally copies the string if given two different arguments.  It
is a little more complicated but it deals with constant strings.  Also
it creates the space if the destination is NULL.

/*
  trim.c
  Written by D'Arcy J.M. Cain
  PlanIX, Inc.

  trims leading and trailing spaces while copying string
  note that destination and source can be the same string
  if dest is NULL then allocate space

*/

#include        <ctype.h>
#include        <stdlib.h>

char *
trim(char *d, const char *s)
{
        char            *dscan;
        const char      *last;

        /* skip leading space */
        while (isspace(*s))
                s++;

        /* find end of string */
        for (last = s; *last; last++)
                ;

        /* find last non-space character */
        while (last > s && isspace(*(last - 1)))
                last--;

        /* if dest is NULL then allocate space */
        if (!d && !(d = malloc((last - s) + 2)))
                return(NULL);

        dscan = d;

        /* copy string */
        while (s < last)
                *dscan++ = *s++;

        *dscan = 0;

        return(d);

Quote:
}

--

PlanIX, Inc.                        |   Thank goodness we don't get
Toronto, Ontario, Canada            |   all the government we pay for.
+1 416 424 2871  (DoD#0082) (eNTP)  |


Fri, 01 Nov 1996 11:46:10 GMT  
 Right trim string function in C

Quote:

>>        I was wondering does anybody knows how or is there
>>a right trim string function available in C?    

>>        Eg. Suppose I have the following:

>>           char *str = "Hello Dolly     \0"

>>        Is there a right trim function that will remove the
>>trailing spaces and make *str looks like "Hello Dolly\0"?
> The standard C run time libraries do not have one. Here is one that
> works ... this better not be solving your homework assignment. Since
> it is pretty late in the semester, I will make this assumption.
> #include <ctype.h>
> #include <stdio.h>
> #include <string.h>
> void
> trim_trailing ( char *s )
> {
>     char *c;
>     if ( s != (char *) NULL )
>     {
>    c = s + strlen ( s ) - 1;
>    while ( c >= s && isspace ( *c ) )
>        --c;
>    *++c = '\0';
>     }
> }

This is not correct.  Comparison of pointers is only valid if they point within
the same array or to the virtual element one past the end of the array.  In
this code c ends up pointing below s and if this is the start of the array
to which s points, the comparision c >= s is invalid.

Change it to

  void
  trim_trailing ( char *s )
  {
      char *c;

      if ( s != (char *) NULL )
      {
        c = s + strlen ( s );
        while ( c > s && isspace ( *(c - 1) ) )
            --c;
        *c = '\0';
      }
  }

--
Mike Rubenstein



Sat, 02 Nov 1996 01:00:03 GMT  
 Right trim string function in C
Mike Rubenstein says:
|

|> #include <ctype.h>
|> #include <stdio.h>
|> #include <string.h>
|
|> void
|> trim_trailing ( char *s )
|> {
|>     char *c;
|
|>     if ( s != (char *) NULL )
|>     {
|>   c = s + strlen ( s ) - 1;
|>   while ( c >= s && isspace ( *c ) )
|>       --c;
|>   *++c = '\0';
|>     }
|> }
|
|This is not correct.  Comparison of pointers is only valid if they point within
|the same array or to the virtual element one past the end of the array.  In
|this code c ends up pointing below s and if this is the start of the array
|to which s points, the comparision c >= s is invalid.

        This does not make any sense at all!  The code works just fine.
        If you're complaining about the fact that 'c' is being dereferenced
        when it is below 's,' you're worrying unnecessarily.
        Thanks to C's "early-out" mechanism, the dereferencing of 'c'
        will never happen when c < s.

---




Sat, 02 Nov 1996 03:59:33 GMT  
 Right trim string function in C

Quote:

>    This does not make any sense at all!  The code works just fine.
>    If you're complaining about the fact that 'c' is being dereferenced
>    when it is below 's,' you're worrying unnecessarily.
>    Thanks to C's "early-out" mechanism, the dereferencing of 'c'
>    will never happen when c < s.

According to ANSI C, you're not even supposed to *compare* two pointers
unless they both point to objects within the same array, or to just beyond
the end of the array.

Your code works fine on your machine.  However, consider an Intel
architecture using the small model, where s happens to be 1FE6:0000.
Decrementing 'c' when c is 1FE6:0000 results in 1FE6:FFFF, because
small-model pointer arithmetic doesn't change the segment field of a
pointer.  'c' won't ever be < s.  Blam!

--
David F. Skoll



Sat, 02 Nov 1996 04:09:46 GMT  
 Right trim string function in C

Quote:
>According to ANSI C, you're not even supposed to *compare* two pointers
>unless they both point to objects within the same array, or to just beyond
>the end of the array.

>Your code works fine on your machine.  However, consider an Intel
>architecture using the small model, where s happens to be 1FE6:0000.
>Decrementing 'c' when c is 1FE6:0000 results in 1FE6:FFFF, because
>small-model pointer arithmetic doesn't change the segment field of a
>pointer.  'c' won't ever be < s.  Blam!

>--
>David F. Skoll

    In small model, an address of 1FE6:0000 isn't even valid anyway!!
That's a NULL pointer, and I assume you wouldn't want to use that, would
you??  But, if you passed it a far pointer (which also does not define
inter-segment pointer arithmetic), then it would not work.  But, this is
getting non-ANSI...

Tim



Sat, 02 Nov 1996 09:09:21 GMT  
 Right trim string function in C

Quote:

>     In small model, an address of 1FE6:0000 isn't even valid anyway!!

If you take the same example in a far model, then it is valid.

However, the point is that the standard does say that one is not
allowed to even generate a pointer that does not point inside an
object, or one past the end.  The beginning of the object does not
count as an end.  The following is invalid:

        char s[10];

        if (s-1 < s)
                printf("s-1 < s");
        else
                printf("s-< >= s");

See the FAQ, 2.16 if I remember correctly.

--

My name is Ozymandias, king of kings/Look on my works, ye Mighty, and despair!



Sat, 02 Nov 1996 13:10:12 GMT  
 Right trim string function in C

Quote:

>     In small model, an address of 1FE6:0000 isn't even valid anyway!!
> That's a NULL pointer,

Not necessarily.  According to the C standard, you can write a null pointer
as the constant 0.  However, the internal representation of NULL is up
to the implementation; if some (weird) C compiler wants to represent NULL
as 1FE6:ABCD, it would be allowed to, providing it did the proper translation
when it casts the integer constant 0 to a NULL pointer and vice-versa.

--
David F. Skoll



Sat, 02 Nov 1996 23:45:36 GMT  
 Right trim string function in C

Mike said:

Quote:
>> |This is not correct.  Comparison of pointers is only valid if they point within
>> |the same array or to the virtual element one past the end of the array.  In
>> |this code c ends up pointing below s and if this is the start of the array
>> |to which s points, the comparision c >= s is invalid.

        I can understand that the <, <=, >, and >= comparisons
        are illegal, but not !=, and ==.

        How else do you search for a particular pointer in a
        linked list, for example.

        I did read K&R's ANSI definition and concur with your
        statement, but it then seems impossible to implement
        the above problem.

---



Sun, 03 Nov 1996 02:21:24 GMT  
 Right trim string function in C

Quote:

> Mike said:
> >> |This is not correct.  Comparison of pointers is only valid if they point within
> >> |the same array or to the virtual element one past the end of the array.  In
> >> |this code c ends up pointing below s and if this is the start of the array
> >> |to which s points, the comparision c >= s is invalid.
>    I can understand that the <, <=, >, and >= comparisons
>    are illegal, but not !=, and ==.
>    How else do you search for a particular pointer in a
>    linked list, for example.
>    I did read K&R's ANSI definition and concur with your
>    statement, but it then seems impossible to implement
>    the above problem.

Right.  I wasn't as precise as I should have been.  The restriction that
the pointers point to elements of the same aggregate (or one past the end)
applies only to <, >, <=, and, >=.  These are called relational operators in
the standard.  

!= and == are called equality operators and are treated in a separate section.
These operators may be used with any pointers to objects as long as the
types are compatible.  Also, equality operators are valid if one of the
pointers points to an object and the other is null or a valid void*.

However, the equality operators cannot be used unless the pointers point to
valid objects or are null, so in the case given where a pointer is decremented
below the start of an array the equality operators would also be undefined.

Of course this doesn't hurt comparing pointers to objects in a linked list for
equality, since each is (or better be) a valid pointer or null.

--
Mike Rubenstein



Sun, 03 Nov 1996 03:28:17 GMT  
 Right trim string function in C

Quote:

> Mike Rubenstein says:
> |

> |> #include <ctype.h>
> |> #include <stdio.h>
> |> #include <string.h>
> |
> |> void
> |> trim_trailing ( char *s )
> |> {
> |>     char *c;
> |
> |>     if ( s != (char *) NULL )
> |>     {
> |>      c = s + strlen ( s ) - 1;
> |>      while ( c >= s && isspace ( *c ) )
> |>          --c;
> |>      *++c = '\0';
> |>     }
> |> }
> |
> |This is not correct.  Comparison of pointers is only valid if they point within
> |the same array or to the virtual element one past the end of the array.  In
> |this code c ends up pointing below s and if this is the start of the array
> |to which s points, the comparision c >= s is invalid.
>    This does not make any sense at all!  The code works just fine.
>    If you're complaining about the fact that 'c' is being dereferenced
>    when it is below 's,' you're worrying unnecessarily.
>    Thanks to C's "early-out" mechanism, the dereferencing of 'c'
>    will never happen when c < s.

If I had been worried about the pointer being dereferenced, I would have
said so.  I clearly said that COMPARISON is not valid in this case.

I can't improve on what David F. Skoll and Lars Wirzenhius said, except to
suggest that you read the standard.  It is very clear on this.  Perhaps it
will make sense to you.

--
Mike Rubenstein



Sat, 02 Nov 1996 23:32:21 GMT  
 
 [ 29 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Way to do trim right in string?

2. String Trim function

3. RTRIM$(right trim)?

4. Help needed with converting Basic TRIM and Right to C equivalents

5. Need help with string functions: LEFT, RIGHT and MID

6. Trim a comma off end of string???

7. Help to trim spaces of a string

8. trim the string

9. String.Trim() behavior

10. String.Trim is this correct functionality

11. String.Trim();

12. Need help to trim spaces off a string

 

 
Powered by phpBB® Forum Software