Gamecocks 2K1
09-13-2001, 09:23 PM
I need to know how to write my own version of the strchr method in c++...HELP!!
|
Click to See Complete Forum and Search --> : strchr in c++ Gamecocks 2K1 09-13-2001, 09:23 PM I need to know how to write my own version of the strchr method in c++...HELP!! Grizzly 09-13-2001, 09:40 PM 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. dighn 09-13-2001, 10:56 PM 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).] Gamecocks 2K1 09-14-2001, 12:07 AM 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; } dighn 09-14-2001, 12:11 AM 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 Gamecocks 2K1 09-14-2001, 12:41 AM 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; } dighn 09-14-2001, 01:52 AM 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).] Humus 09-14-2001, 08:05 AM dighn: May I ask why you're writing it for(;*c!='\0';c++) if(*c == c2)return (char*)(c); instead of the more natural and much more readable while (c != '\0'){ if (*c == c2) return (char*) c; c++; } ------------------ Tweak your Radeon. Get Raid on Tweaker here (http://hem.passagen.se/emiper/Radeon.html) Gamecocks 2K1 09-14-2001, 01:03 PM 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 for(;*c!='\0';c++) if(*c == c2)return (char*)(c); instead of the more natural and much more readable while (c != '\0'){ if (*c == c2) return (char*) c; c++; } Zoma 09-14-2001, 06:56 PM Or the most readable version of all: 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; } http://www.sharkyforums.com/ubb/smile.gif Humus 09-14-2001, 07:16 PM 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; } http://www.sharkyforums.com/ubb/smile.gif[/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 (http://hem.passagen.se/emiper/Radeon.html) [This message has been edited by Humus (edited September 14, 2001).] dighn 09-14-2001, 10:55 PM Humus: LOL http://www.sharkyforums.com/ubb/smile.gif it seemed pretty natural and readable to me so i dunno http://www.sharkyforums.com/ubb/wink.gif i thought about writing it the other way but i didn't want to change it anymore http://www.sharkyforums.com/ubb/biggrin.gif and Gamecocks 2K1 I'm sorry the code didn't work for you. You are obviously doing some else wrong because that code compiles fine for me... ------------------ Keep it brief SharkyExtreme.com
Copyright Internet.com Inc. All Rights Reserved. |