For some reason, whenever I try to find a slash at the end of a byte string (in this case, base_url which is set to "www.google.com/"), it doesn't seem to want to return that it is or isn't there. However, if I change base_url to something like "www.google.com" where m is the last character, then it finds it just fine.

//in this case, the base_url is "www.google.com/". I also tried to simply put //"www.google.com/" in place of the base_url, just in case.

char * last_char_ptr;
last_char_ptr = strrchr(base_url, '/');

if(last_char_ptr == NULL)
     cout << "it is null!" << endl;
     cout << *last_char_ptr << endl;


6 Years
Discussion Span
Last Post by freedomflyer

It's unlikely that what you describe is happening. Consider

#include <cstring>
#include <cstdio>

const char url[] = "www.google.com/";

int main () {
    char * pos = strrchr (url, '/');
    if (pos) {
        printf ("Found / in %s\n", url);
    return 0;

I've run that 100 times each time the '/' is found. It is most likely the case that you are not getting the input you expect.

Edited by L7Sqr: n/a

Votes + Comments
Showed some effort behind his work.

You are right. However, the input was correct - I simply misinterpreted the meaning of the strrchr function - I thought it only searched the last character and matched it, but instead it searches the entire cstring and matches the last OCCURRENCE of the given character, yielding a much different result. Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.