STRREV(3pub)

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

okay bestie, welcome to the C Programmer's Manual, section 3pub β€” this is the publib corner of the library, and today's main character is a little function called strrev ✨


🌸 NAME 🌸

strrev β€” flips a string around, right there in place, no copy, no drama πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <publib.h>
char *strrev(char *str);

✨ DESCRIPTION ✨

so strrev reverses the string you hand it, and it does it in place β€” meaning it literally swaps the *i*th character counting from the front with the *i*th character counting from the back. no new buffer, no sneaky allocation, just vibes and swaps 🫢


πŸ‘‘ RETURN VALUE πŸ‘‘

strrev returns its argument. that's it, that's the whole return. it gives you back the same pointer you gave it, but now the string is backwards 😭 iconic behaviour honestly


πŸŽ€ EXAMPLE πŸŽ€

say you wanted to reverse "dlrow, elloh" β€” you'd do it like this:

char str[] = "dlrow, elloh";

puts(strrev(str));

and this would output "hello, world" 🌈

⚠️ heads up bestie: using a string literal as the argument would be an error, because it is not allowable to modify string literals. like, do not do that. she will not survive it πŸ’€

πŸ’€ BUGS πŸ’€

okay so the tea: strrev does not automatically detect palindromes, and it also does not automatically return without doing anything. so if you were hoping for a lil self-aware function that goes "omg this is already the same backwards, I'll just chill" β€” nope. she does not do that. not in her skill set πŸ™ˆ


πŸ”₯ SEE ALSO πŸ”₯


πŸ’œ AUTHOR πŸ’œ

Lars Wirzenius (lars.wirzenius@helsinki.fi)


and that's the whole page, bestie!! πŸŽ€ this was STRREV(3pub) from the Publib collection, chillin' in the C Programmer's Manual β€” mwah, go reverse something responsibly, ily πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from