Delete "/" from char pointer 
Author Message
 Delete "/" from char pointer

Hi,
can anyone tell me please, how I can check if a "/" character is at the
end of a pointer? And how I can delete it from the pointer?
Example:
if the char pointer is "test/" then it should be only "test".

Thx,
Axel



Mon, 12 Jul 2004 05:54:26 GMT  
 Delete "/" from char pointer

Quote:

>  can anyone tell me please, how I can check if a "/" character is at the
>  end of a pointer?

I think you meant "string" where you wrote "pointer". In that case, use
strrchr().

Quote:
> And how I can delete it from the pointer?

If the string is modifiable, and the '/' is the very last character in
it, simply replace it by the null character '\0'.
If the slash is not the last character, you probably want to move the
rest of the string one character to the front; this can be done
manually, or with memmove(). Do *not* use strcpy() or memcpy() for this
purpose, as their behavior is undefined if the source and destination
arrays overlap.

Gergo
--
Men have as exaggerated an idea of their rights as women have of their wrongs.
                -- E.W. Howe



Mon, 12 Jul 2004 06:03:54 GMT  
 Delete "/" from char pointer

126743.news.dfncis.de:

Quote:
> can anyone tell me please, how I can check if a "/" character is at the
> end of a pointer? And how I can delete it from the pointer?
> Example:
> if the char pointer is "test/" then it should be only "test".

Please don't mix 'string' or 'string literal', which are arrays of char
terminated by a 0 and a pointer to such animals, which is simply a
variable of type char * that yelds the address of the array.

If you are looking for the last occurance of a character in a string, you
want strrchr() as declared in <string.h>.

http://www.dinkumware.com/htm_cl/string.html#strrchr

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Mon, 12 Jul 2004 06:26:40 GMT  
 Delete "/" from char pointer

Quote:

>can anyone tell me please, how I can check if a "/" character is at the
>end of a pointer? And how I can delete it from the pointer?

I think you mean string--or more pedantically, array of char.

Quote:
>Example:
>if the char pointer is "test/" then it should be only "test".

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

#define LASTCHAR(s)     (s)[strlen(s) - 1]

int main(void)
{
        char foo[] = "test/";

        printf("Before: foo=\"%s\"\n", foo);

        /* If '/' is the last char in the string, delete it
         * by overwriting it with a NUL character.
         */

        if (LASTCHAR(foo) == '/')
                LASTCHAR(foo) = '\0';

        printf("After : foo=\"%s\"\n", foo);
        return 0;

Quote:
}

Other tools that may be helpful are strrchr(), which will return a pointer
to the _last occurrence_ of a char within the string.  This is not
necessarily the character at the _end_ of the string.

For deleting a character within the string at offset n, you could always
manually copy the remainder of the string (n+1...) to position n, or you
could use memmove() to take care of this for you.
--
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://home.earthlink.net/~rclark31/ClarkWehyr.html



Mon, 12 Jul 2004 07:48:35 GMT  
 Delete "/" from char pointer
On Wed, 23 Jan 2002 23:48:35 GMT, Robert B. Clark

Quote:


>>can anyone tell me please, how I can check if a "/" character is at the
>>end of a pointer? And how I can delete it from the pointer?

>I think you mean string--or more pedantically, array of char.

>>Example:
>>if the char pointer is "test/" then it should be only "test".

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

>#define LASTCHAR(s) (s)[strlen(s) - 1]

What if strlen(s) == 0?

Quote:

>int main(void)
>{
>    char foo[] = "test/";

>    printf("Before: foo=\"%s\"\n", foo);

>    /* If '/' is the last char in the string, delete it
>     * by overwriting it with a NUL character.
>     */

>    if (LASTCHAR(foo) == '/')
>            LASTCHAR(foo) = '\0';

This seems a little inefficient, since you compute the length of the
string twice.

How about this for the macro:

#define LASTCHAR(S) (*(S) ? strchr((S),0)-1 : NULL)

Then:

char *lc=LASTCHAR(foo);

if (lc && *lc=='/')
   *lc=0;

Or, you could build on the LASTCHAR macro with another macro to
conditionally delete the last character:

#define DELETE_LASTCHAR_IF(S,C) \
    do { \
        char *lc=LASTCHAR((S)); \
        if (lc && *lc==(C)) \
            *lc=0; \
    } while (0)

Russ



Mon, 12 Jul 2004 08:32:51 GMT  
 Delete "/" from char pointer
On Wed, 23 Jan 2002 21:54:26 GMT, "Axel Steiner"


Quote:
> can anyone tell me please, how I can check if a "/"
> character is at the end of a pointer? And how I can delete
> it from the pointer? Example:
> if the char pointer is "test/" then it should be only
> "test".

#include <string.h>
void chomp_slash (char* s)
{
   char* p = s + (strlen(s) - 1);
   if ('/' == *p) *p = '\0';

Quote:
}

--

Umesh P Nair
Remove 'z's from my e-mail ID



Mon, 12 Jul 2004 08:35:29 GMT  
 Delete "/" from char pointer

Quote:
> On Wed, 23 Jan 2002 21:54:26 GMT, "Axel Steiner"


> > can anyone tell me please, how I can check if a "/"
> > character is at the end of a pointer? And how I can delete
> > it from the pointer? Example:
> > if the char pointer is "test/" then it should be only
> > "test".

> #include <string.h>
> void chomp_slash (char* s)
> {
>    char* p = s + (strlen(s) - 1);
>    if ('/' == *p) *p = '\0';
> }

This breaks if s has length 0.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz


Mon, 12 Jul 2004 08:56:31 GMT  
 Delete "/" from char pointer

Quote:

<snip>

> #include <string.h>
> void chomp_slash (char* s)
> {
>    char* p = s + (strlen(s) - 1);
>    if ('/' == *p) *p = '\0';
> }

Excellent. Just one question, though - what if s is "" ? :-)

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Mon, 12 Jul 2004 09:09:34 GMT  
 Delete "/" from char pointer
On Thu, 24 Jan 2002 01:09:34 GMT, Richard Heathfield


Quote:

> <snip>

>> #include <string.h>
>> void chomp_slash (char* s)
>> {
>>    char* p = s + (strlen(s) - 1);
>>    if ('/' == *p) *p = '\0'; }

> Excellent. Just one question, though - what if s is "" ? :-)

UB. (Hang me! Hang me! Hang me!.)

Thanks, Ben & Richard.

Corrected version:

#include <string.h>
void chomp_slash (char* s)
{
   if (s && *s) { /* Testing for NULL too :-) */
      char* p = s + (strlen(s) - 1);
      if ('/' == *p) *p = '\0';
   }

Quote:
}

--

Umesh P Nair
Remove 'z's from my e-mail ID



Mon, 12 Jul 2004 12:51:47 GMT  
 Delete "/" from char pointer
Thanks! That what I'm looking for.

Axel



Quote:
> #include <string.h>
> void chomp_slash (char* s)
> {
>    if (s && *s) { /* Testing for NULL too :-) */
>       char* p = s + (strlen(s) - 1);
>       if ('/' == *p) *p = '\0';
>    }
> }



Mon, 12 Jul 2004 18:52:20 GMT  
 Delete "/" from char pointer

Quote:
>On Wed, 23 Jan 2002  GMT, "Axel Steiner" wrote in
>> can anyone tell me please, how I can check if a "/"
>> character is at the end of a pointer? And how I can delete
>> it from the pointer? Example:
>> if the char pointer is "test/" then it should be only
>> "test".

>#include <string.h>
>void chomp_slash (char* s)
>{
>   char* p = s + (strlen(s) - 1);
>   if ('/' == *p) *p = '\0';
>}

char* chomp_sl1(char* s)
{char * p=s;
 if(s==NULL || *s=='\0') return p;
 while(*s) s++;
 if(*--s=='/' ) *s = '\0';
 return p;
Quote:
}

_____________________________

Is this correct?

char* chomp_sl2(char* s)
{char * p=s;
 if( !s || !*s) return p;
 while(*s) s++;
 while(*--s=='/' ) *s = '\0';
 return p;

Quote:
}



Mon, 12 Jul 2004 23:23:56 GMT  
 Delete "/" from char pointer

Quote:

>On Wed, 23 Jan 2002 23:48:35 GMT, Robert B. Clark

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

>>#define LASTCHAR(s)     (s)[strlen(s) - 1]

>What if strlen(s) == 0?

Um, UB? <g>

You are, of course, correct.  Thanks for pointing it out.

--
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://home.earthlink.net/~rclark31/ClarkWehyr.html



Tue, 13 Jul 2004 00:27:29 GMT  
 Delete "/" from char pointer

Quote:


>>On Wed, 23 Jan 2002  GMT, "Axel Steiner" wrote in
>>> can anyone tell me please, how I can check if a "/"
>>> character is at the end of a pointer? And how I can delete
>>> it from the pointer? Example:
>>> if the char pointer is "test/" then it should be only
>>> "test".

>>#include <string.h>
>>void chomp_slash (char* s)
>>{
>>   char* p = s + (strlen(s) - 1);
>>   if ('/' == *p) *p = '\0';
>>}

>char* chomp_sl1(char* s)
>{char * p=s;
> if(s==NULL || *s=='\0') return p;
> while(*s) s++;
> if(*--s=='/' ) *s = '\0';
> return p;
>}

This looks ok.

Quote:
>_____________________________

>Is this correct?

>char* chomp_sl2(char* s)
>{char * p=s;
> if( !s || !*s) return p;
> while(*s) s++;
> while(*--s=='/' ) *s = '\0';

You invoke undefined behavior here if the entire string is composed of
'/' characters as you end up pointing s before the beginning of the
supplied string. You can fix it by changing it to this:

while(s!=p && *--s=='/' ) *s = '\0';

Quote:
> return p;
>}

Russ


Tue, 13 Jul 2004 06:33:59 GMT  
 Delete "/" from char pointer

Quote:

> Hi,
> can anyone tell me please, how I can check if a "/" character is at the
> end of a pointer? And how I can delete it from the pointer?
> Example:
> if the char pointer is "test/" then it should be only "test".

> Thx,
> Axel

One more option...

  char test[] = "test/";

  {
    char *slash;
    if ((slash = strrchr(test, '/')) != NULL && slash[1] == 0)
      *slash = 0;
  }

--
Peter



Tue, 13 Jul 2004 09:57:56 GMT  
 Delete "/" from char pointer

Quote:


>>Is this correct?

I imagined something wrong  (i forced my c language)

Quote:
>> while(*--s=='/' ) *s = '\0';

>You invoke undefined behavior here if the entire string is composed of
>'/' characters

Thank you


Wed, 14 Jul 2004 01:39:22 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. char foo[]=""; vs char* foo="";

2. char a[] = "abc"; char* a = "abc";

3. Difference between "char *arr" and "char arr[]"

4. difference between "char*" and "const char*"

5. Why can only undo "Delete"?

6. Deleting with "fwrite()"

7. ODBC, VFP and "real" deletes

8. how to use "delete"?

9. "delete []" in MSDN

10. Question about the operator "delete"

11. "scalar deleting destructor"

12. C++ question with "delete"

 

 
Powered by phpBB® Forum Software