By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

best way to chop off leading char in string?

P: n/a
I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url[i];

is this the best way to do this or is there a more efficient way?

Thanks,
Aaron

Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Aaron Walker" <ka*****@REMOVETHIScfl.rr.com> wrote in message
news:GJ*******************@twister.tampabay.rr.com ...
I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url[i];

is this the best way to do this or is there a more efficient way?


char input[] = "/filename.html"; /* or 'malloc()' it if you prefer */
interface_func(input + 1);

:-)

-Mike
Nov 13 '05 #2

P: n/a
On Sat, 22 Nov 2003 19:24:22 +0000, Aaron Walker wrote:
I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));
Umm... no. Don't cast malloc. It's unneccessary and can hide bugs.
for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url[i];


You might want to try memmove. It can cope with overlapping regions, so
you could use the same buffer for the source and the destination. If you
really want a secondary buffer (i.e. leave the original data intact) the
obvious answer would be to simply use strcpy:

char *removeleadingchar( const char *src )
{
char *dst = malloc( strlen( src ) );
strcpy( dst, src + 1 );
return dst;
}

Note you should check for NULLs, check that src is long enough, not NULL,
etc.

Nov 13 '05 #3

P: n/a
On Sat, 22 Nov 2003 19:24:22 +0000, Aaron Walker wrote:
I am attempting to write a http server, and the requested url is always
a string like "/filename.html" for example. I need to strip off the '/'
char before I can attempt to open that file, obviously.

Right now, I have the following code:

/* url is previously declared (of type char *) */
int i, c;
char *url_p;

url_p = (char *) malloc(strlen(url));

for(i = 1, c = 0; i <= strlen(url); i++, c++)
url_p[c] = url[i];

is this the best way to do this or is there a more efficient way?


If the only thing you want to do with the "stripped" string is
to pass it to fopen() or something like that, then you can just
do:

#include <stdio.h>
...
FILE * url_file;
...
url_file = fopen(url+1, "r");
if (url_file) ... else ...

url+1 is a pointer to the second character in the string, 'f' in your
example above. Obviously you can't do this if you want to alter the
"stripped" string without also altering the original string.

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.