strchr in c++

Sharky Forums


Results 1 to 12 of 12

Thread: strchr in c++

  1. #1
    Expensive Sushi
    Join Date
    Sep 2001
    Location
    Columbia, SC, USA
    Posts
    9

    Exclamation strchr in c++

    I need to know how to write my own version of the strchr method in c++...HELP!!

  2. #2
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    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.

  3. #3
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    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).]
    .

  4. #4
    Expensive Sushi
    Join Date
    Sep 2001
    Location
    Columbia, SC, USA
    Posts
    9

    Exclamation

    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;
    }

  5. #5
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    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
    .

  6. #6
    Expensive Sushi
    Join Date
    Sep 2001
    Location
    Columbia, SC, USA
    Posts
    9

    Post

    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;
    }

  7. #7
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    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).]
    .

  8. #8
    Hammerhead Shark
    Join Date
    Sep 2000
    Location
    Luleå, Sweden
    Posts
    1,921

    Post

    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
    Get UniTuner here

  9. #9
    Expensive Sushi
    Join Date
    Sep 2001
    Location
    Columbia, SC, USA
    Posts
    9

    Exclamation

    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++;
    }


  10. #10
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    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 |

  11. #11
    Hammerhead Shark
    Join Date
    Sep 2000
    Location
    Luleå, Sweden
    Posts
    1,921

    Post

    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).]
    Get UniTuner here

  12. #12
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    Humus: LOL
    it seemed pretty natural and readable to me so i dunno i thought about writing it the other way but i didn't want to change it anymore

    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
    .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •