STRRSTR(3pub)

πŸ’– STRRSTR(3pub) πŸ’–

okay bestie, welcome to the C Programmer's Manual β€” yes it's giving very serious library energy but we're gonna make it cute. this one is strrstr from publib, and she's basically the "find the LAST one" girlie. iykyk. ✨


🌸 NAME 🌸

strrstr β€” locate last occurrence of substring πŸ’…

like. she finds the last place a lil substring shows up. that's the whole gig. periodt.


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <publib.h>
char *strrstr(const char *str, const char *pat);

no cap, that's it. include the header, call the function, live your truth. 🌈


πŸŽ€ DESCRIPTION πŸŽ€

so strrstr goes hunting for the last occurrence of the string pat inside the string str. not the first one, bestie β€” the LAST one. it's giving "we checked the whole thing and this is the final spot."

and listen: the terminating '\0' characters are not compared. they're just there vibing at the end of the strings, doing their lil null-terminator thing, not participating in the matching. πŸ™ˆ


⭐ RETURN VALUE ⭐

she hands you back a pointer to the first character of that substring β€” iconic behavior honestly.

but if the substring is not found? you get NULL. no substring, no pointer. that's the deal. πŸ’€


🌟 EXAMPLE 🌟

okay so the vibe here: you wanna print out everything on each line starting with the last occurrence of "/* " on each line. very "grab me the tail end of this comment" energy. you might use the following code:

#include <stdio.h>
#include <publib.h>

int main(void) {
     char *p, line[512];

     while (fgets(line, sizeof(line), stdin) != NULL) {
          p = strrstr(line, "/* ");
          if (p != NULL)
               printf("%s", p);
     }
     return 0;
}

she reads lines, finds the last "/* ", and if it's there, prints from that point onward. slay. πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, bestie, they're all in the same friend group. 🫢


πŸ’œ AUTHOR πŸ’œ

Lars Wirzenius (lars.wirzenius@helsinki.fi) β€” the main character who wrote this. we love her. πŸ‘‘


and that's the whole page, fr fr! this gem came to you from Publib, in the C Programmer's Manual, under STRRSTR(3pub) β€” dated way back when the manual pages were young. stan. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from