find the last occurence of char in string WITHOUT the use of strrchr()

This is a discussion on find the last occurence of char in string WITHOUT the use of strrchr() within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a function that locate the last occurence of a string without using string library. ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    find the last occurence of char in string WITHOUT the use of strrchr()

    Hi, I am trying to write a function that locate the last occurence of a string without using string library. Also, i would like to use pointers but I am getting "Segmentation Fault" every time i run the program.

    heres my function. Thanks in advance guys.

    Code:
    int str_char(char c, char *str)
    {
        int i;
        int last = 0;
        char *strptr = str;
        while (*strptr != '\0') {
            if (strptr[i] == c)
                last = i;
        }
        return last;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are not changing i (and you either shoud use i to index into strptr in both places, or change strptr forward and not use index).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Try to decrement strptr until it reaches the beginning or points to the desired char. Then somehow calculate the index of the character. Or use you own way and increment i. But I think it'd be better to search backward.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brafil View Post
    Try to decrement strptr until it reaches the beginning or points to the desired char. Then somehow calculate the index of the character. Or use you own way and increment i. But I think it'd be better to search backward.
    Actually makes fairly little difference. If the string is very long (hundreds of characters) then it may be of benefit to do strlen first - but strlen in itself has to read every character, so it's not MUCH benefit - the main benefit is that when you read from the back, you can stop immediately as you have found the target character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Ooops, forgot this issue with strlen. And you have to check if strptr is smaller than str every time.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    You probably also want to return something other than 0 if you don't find the character. 0 would be returned if the last instance of the character was at the start of the string as well and you don't have a way to tell them apart.

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Just a hint: Since the result can only be positive, return a negative value on failure (-1).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21