|
-
strchr in c++
I need to know how to write my own version of the strchr method in c++...HELP!!
-
Ursus Arctos Moderatis
strchr simply returns the index of the array where your character is found right?
Just loop through the array until you find the character in question, and return the current index you're at.
-
Mako Shark
Originally posted by Gamecocks 2K1:
I need to know how to write my own version of the strchr method in c++...HELP!!
somewhat like this:
char* strchr(const char* c, char c2)
{
for(;*c!='\0';c++)
if(*c == c2)return c;
return 0;
}
------------------
Keep it brief
[This message has been edited by dighn (edited September 13, 2001).]
-
I am getting an error in the return statement contained in the if loop...i must be an idiot to not see what I'm doing wrong here...any help would be greatly appreciated. Thanks for your help, dighn.
Originally posted by dighn:
somewhat like this:
char* strchr(const char* c, char c2)
{
for(;*c!='\0';c++)
if(*c == c2)return c;
return 0;
}
-
Mako Shark
my fault... oops
try this
char* strchr(const char* c, char c2)
{
for(;*c!='\0';c++)
if(*c == c2)return const_cast<char*>(c);
return 0;
}
------------------
Keep it brief
-
I keep getting an error that says that I have symbol referencing errors. Any hints? Thanks.
Originally posted by dighn:
my fault... oops
try this
char* strchr(const char* c, char c2)
{
for(;*c!='\0';c++)
if(*c == c2)return const_cast<char*>(c);
return 0;
}
-
Mako Shark
Originally posted by Gamecocks 2K1:
I keep getting an error that says that I have symbol referencing errors. Any hints? Thanks.
are you using an fairly modern c++ compiler? older ones might not be able to handle the c++ style cast, try this:
char* strchr(const char* c, char c2)
{
for(;*c!='\0';c++)
if(*c == c2)return (char*)(c);
return 0;
}
------------------
Keep it brief
[This message has been edited by dighn (edited September 14, 2001).]
-
dighn:
May I ask why you're writing it
Code:
for(;*c!='\0';c++)
if(*c == c2)return (char*)(c);
instead of the more natural and much more readable
Code:
while (c != '\0'){
if (*c == c2) return (char*) c;
c++;
}
------------------
Tweak your Radeon. Get Raid on Tweaker here
-
Well, I'm using the g++ compiler in a Unix environment, and it is still giving the same symbol referencing error as before even after trying the two other implementations mentioned. Oh well. Thank you guys for taking the time to help me out a little bit.
Originally posted by Humus:
dighn:
May I ask why you're writing it
Code:
for(;*c!='\0';c++)
if(*c == c2)return (char*)(c);
instead of the more natural and much more readable
Code:
while (c != '\0'){
if (*c == c2) return (char*) c;
c++;
}
-
Or the most readable version of all:
Code:
char *strchr(const char *c, char c2)
{
while (*c != NULL)
{
if (*c == c2)
{
return c;
}
c++;
}
return NULL;
}
OR
char *strchr(const char *c, char c2)
{
while (*c != NULL)
{
if (*c == c2)
{
break;
}
c++;
}
return c;
}
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
-
Code:
char *strchr(const char *c, char c2)
{
while (*c != NULL)
{
if (*c == c2)
{
return c;
}
c++;
}
return NULL;
}
OR
char *strchr(const char *c, char c2)
{
while (*c != NULL)
{
if (*c == c2)
{
break;
}
c++;
}
return c;
}
[/b]<HR></BLOCKQUOTE>
Well, I'm not sure that
if (*c == c2)
{
return c;
}
is any better than
if (*c == c2) return c;
It only makes the code larger without making it more readable IMO.
And the comparion *c != NULL isn't really that good. While it will work it does a comparison between a char and a pointer, which doesn't make much sense. *c != '\0', or even c != 0 is much better.
------------------
Tweak your Radeon. Get Raid on Tweaker here
[This message has been edited by Humus (edited September 14, 2001).]
-
Mako Shark
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|