// strrchr example // locate last occurrence of character in string // source: http://www.cplusplus.com/ #include #include #include int main() { char str[] = "But the students did bring the servers down all of a sudden"; char *pch; pch=strrchr(str,'s'); // we use pointers here. strrchr returns a pointer to the last s // we substract that from the str address and add 1. makes sense? printf("pointers str = %p and pch = %p and the difference (pch-str) = %p\n", str, pch,(void *)(pch-str+1)); fprintf(stdout, "Last occurence of an 's' found in the string \"%s\" is at location %d\n", str, (int)(pch-str+1)); return EXIT_SUCCESS; }