trimming a string 
Author Message
 trimming a string

C savvy,

        I'd like to know how to trim out heading & ending white spaces of a
string in C.

        Appriciate your help.

--

========
Daisy Wu



Mon, 30 Apr 2001 03:00:00 GMT  
 trimming a string
C savvy,

        I'd like to know how to trim out heading & ending white spaces of a
string in C.

        Appriciate your help.

--
--
Daisy



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string
Hi,
How about this

 char *ltrim(char *s1)
  {
  int i,j;
  for(i=0;s1[i]==' ' || s1[i]=='\t' || s1[i]== '\n';i++);
  for(j=0;s1[j++]=s1[i++];);
  return s1;
  }

  char *rtrim(char *s1)
  {
  int i,j;
  for(i=0;s1[i];i++);
  for(--i;s1[i]==' ' || s1[i]=='\t' || s1[i]== '\n';i--);
  s1[++i]='\0';
  return s1;
  }

V. Raghavendra Holla.


| C savvy,
|
|       I'd like to know how to trim out heading & ending white spaces of a
| string in C.
|
|       Appriciate your help.
|
|
| --
|
| ========
| Daisy Wu
|



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string
isspace()
sanity checks

--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string

Quote:
> I'd like to know how to trim out heading & ending white spaces of a
> string in C.

Here's one way of doing it.  You should probably look for a different
way.

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

char * trim (char *);
static char * trounce (char *);
static char * denounce (char *);
static char * renounce (char *);
static char * pounce (char *, char *);
static char * bounce (char *, char *, char *);

char *
trim (char *string)
{
  return trounce (trounce (string));

Quote:
}

static char *
trounce (char *string)
{
  return renounce (denounce (string));

Quote:
}

static char *
denounce (char *string)
{
  return isspace (*string) ? denounce (string+1) : string;

Quote:
}

static char *
renounce (char *string)
{
  return *string ? pounce (string, string) : string;

Quote:
}

static char *
pounce (char *string, char *first)
{
  return *(string+1)
         ? pounce (string + 1, first)
         : bounce (first, first, string);

Quote:
}

static char *
bounce (char *string, char *first, char *last)
{
  return (first < last)
         ? *first ^= *last, *last ^= *first, *first ^= *last,
           bounce (string, first + 1, last - 1)
         : string;

Quote:
}

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis
Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string

Quote:

> |
> |       I'd like to know how to trim out heading & ending white spaces of a
> | string in C.
> How about this

>  char *ltrim(char *s1)
>   {
>   int i,j;
>   for(i=0;s1[i]==' ' || s1[i]=='\t' || s1[i]== '\n';i++);
>   for(j=0;s1[j++]=s1[i++];);
>   return s1;
>   }

>   char *rtrim(char *s1)
>   {
>   int i,j;
>   for(i=0;s1[i];i++);
>   for(--i;s1[i]==' ' || s1[i]=='\t' || s1[i]== '\n';i--);
>   s1[++i]='\0';
>   return s1;
>   }

 These functions generally work but could lead to disaster
should you have as an argument, a string of white space characters
only. In this scenario, You will go beyond the boundary of the
char array.

I would modify this to:
#include <stdio.h>
#include <ctype.h>

 char *ltrim(char *s1)
  {
  int i,j;
  for(i = 0 ;isspace(s1[i]);i++) if(s1[i] == '\0') break;
  for(j=0;s1[j++]=s1[i++];) ;
  return s1;
  }

  char *rtrim(char *s1)
  {
  int i;
  for(i=0;s1[i];i++);
  for(--i;isspace(s1[i]);i--) if(i == 0) {i--;break;}
  s1[++i]='\0';
  return s1;
  }

int main(void) {
   char buf[32] = "   \r\r\n\n  sing \v\v\t\t\f\f   ";
   rtrim(buf);
   ltrim(buf);
   printf("%s-%s\n",buf,buf);
   return 0;
   }|
--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string

Quote:

> Hi,
> How about this

>  char *ltrim(char *s1)
>   {
>   int i,j;
>   for(i=0;s1[i]==' ' || s1[i]=='\t' || s1[i]== '\n';i++);
>   for(j=0;s1[j++]=s1[i++];);
>   return s1;
>   }

>   char *rtrim(char *s1)
>   {
>   int i,j;
>   for(i=0;s1[i];i++);
>   for(--i;s1[i]==' ' || s1[i]=='\t' || s1[i]== '\n';i--);
>   s1[++i]='\0';
>   return s1;
>   }

You could use isspace() instead - much cleaner.

--
 { Sunil Rao }
"And India acquired yet another willing convert to the philosophy of
 the meaningfully meaningless...  Or was it the meaninglessly
 meaningful?  Did anyone know what was happening?"  --  Gita Mehta



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string

Quote:

> C savvy,

>         I'd like to know how to trim out heading & ending white spaces of a
> string in C.

>         Appriciate your help.

Hi Daisy,

Here's a simply one-liner that will do it, if you can live with
not actually moving the string contents:

   char myString[] = "   Test Test Test   ";
   char *newString;
   newString = strtok( myString, " " );
   if ( newString == NULL )
      /* you can deal with just-whitespace strings here */

Leading whitespace will be skipped: "newString" is set to the
first non-whitespace character. Trailing whitespace are removed
by replacing the first trailing whitespace with '\0'.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string
Ok, I assume that you have a null terminate string.

For the leading blanks:
while ( myString[0] != '\0' && myString[0] == ' ' )
    strcpy( myString, &myString[1] );

For the trailing blanks:
int nIndex = 0;
...
nIndex = strlen( myString ) - 1;
while ( nIndex >= 0 && myString[nIndex] == ' ' )
{
    myString[nIndex] = '\0';
    nIndex--;

Quote:
}

> C savvy,

>         I'd like to know how to trim out heading & ending white spaces of a
> string in C.

>         Appriciate your help.

> --

> ========
> Daisy Wu

--
Nicolas Thomassin
Consultant, Partner
CGI Information Systems and Management Consultants Inc.


Tue, 01 May 2001 03:00:00 GMT  
 trimming a string
: >
: > C savvy,
: >
: >         I'd like to know how to trim out heading & ending white spaces of a
: > string in C.
: >
: >         Appriciate your help.

: Hi Daisy,

: Here's a simply one-liner that will do it, if you can live with
: not actually moving the string contents:

:    char myString[] = "   Test Test Test   ";
:    char *newString;
:    newString = strtok( myString, " " );
:    if ( newString == NULL )
:       /* you can deal with just-whitespace strings here */

: Leading whitespace will be skipped: "newString" is set to the
: first non-whitespace character. Trailing whitespace are removed
: by replacing the first trailing whitespace with '\0'.

I think she wants the answer "Test Test Test", not "Test".

Will



Tue, 01 May 2001 03:00:00 GMT  
 trimming a string

Quote:

> >         I'd like to know how to trim out heading & ending white spaces of a
> > string in C.

> Here's a simply one-liner that will do it, if you can live with
> not actually moving the string contents:

>    char myString[] = "   Test Test Test   ";
>    char *newString;
>    newString = strtok( myString, " " );
>    if ( newString == NULL )
>       /* you can deal with just-whitespace strings here */

> Leading whitespace will be skipped: "newString" is set to the
> first non-whitespace character. Trailing whitespace are removed
> by replacing the first trailing whitespace with '\0'.

Not really. The ending spaces will never even be reached in your
example. newString will point to a string  "Test".
The following code will demonstrate and you will see
if fails to perform as specified by the poster.

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

int main(void) {
   char myString[] = "   Test Test Test   ";
   char *newString;
   newString = strtok( myString, " " );
   if ( newString == NULL ) exit(EXIT_FAILURE);
   puts(newString);
   return 0;
   }

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Wed, 02 May 2001 03:00:00 GMT  
 trimming a string

Quote:

> For the leading blanks:
> while ( myString[0] != '\0' && myString[0] == ' ' )
>     strcpy( myString, &myString[1] );

You cannot do this. strcpy()  results in behavior thatis undefined if copying
takes place between
overlapping objects.

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Wed, 02 May 2001 03:00:00 GMT  
 trimming a string

Quote:


> > C savvy,

> >         I'd like to know how to trim out heading & ending white spaces of a
> > string in C.

> >         Appriciate your help.

> Hi Daisy,

> Here's a simply one-liner that will do it, if you can live with
> not actually moving the string contents:

>    char myString[] = "   Test Test Test   ";
>    char *newString;
>    newString = strtok( myString, " " );
>    if ( newString == NULL )
>       /* you can deal with just-whitespace strings here */

> Leading whitespace will be skipped: "newString" is set to the
> first non-whitespace character.

But that isn't what the original poster wanted, is it??? You've only
gone half-way.

--
 { Sunil Rao }
"And India acquired yet another willing convert to the philosophy of
 the meaningfully meaningless...  Or was it the meaninglessly
 meaningful?  Did anyone know what was happening?"  --  Gita Mehta



Wed, 02 May 2001 03:00:00 GMT  
 trimming a string


Quote:

>> For the leading blanks:
>> while ( myString[0] != '\0' && myString[0] == ' ' )
>>     strcpy( myString, &myString[1] );

>You cannot do this. strcpy()  results in behavior thatis undefined if copying
>takes place between
>overlapping objects.

I thought it was only undefined for cases where the destination
address is greater than the source address.  I've done overlapping
strcpy where dest < src many, many times without getting unexpected
results.  I'd hate to find out *now* that it was just good luck!


Wed, 02 May 2001 03:00:00 GMT  
 trimming a string

Quote:




>>> For the leading blanks:
>>> while ( myString[0] != '\0' && myString[0] == ' ' )
>>>     strcpy( myString, &myString[1] );

>>You cannot do this. strcpy()  results in behavior thatis undefined if copying
>>takes place between
>>overlapping objects.

>I thought it was only undefined for cases where the destination
>address is greater than the source address.  I've done overlapping
>strcpy where dest < src many, many times without getting unexpected
>results.  I'd hate to find out *now* that it was just good luck!

It was bad luck.  It seemed to work, so you didn't realize it was
incorrect :-)

Undefined behavior can be anything, including sometimes being
something reasonable.
--
Michael M Rubenstein



Wed, 02 May 2001 03:00:00 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. trim the string

2. How to trim a string in a _variant_t

3. How to trim a String?

4. trim for strings

5. Is there any easier way to trim the string?

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

7. Help to trim spaces of a string

8. String.Trim() behavior

9. String.Trim is this correct functionality

10. String.Trim();

11. Need help to trim spaces off a string

12. Right trim string function in C

 

 
Powered by phpBB® Forum Software