Programming Reference/Librarys
Question & Answer
Q&A is closed
strrchr () is a string function that finds the last occurrence of a character in a string. As a result, the pointer comes to this character or the null pointer if this character is not found.
In the following example, a string is read by fgets. fgets hangs at the end of a New-line character (\ n). We are looking for a pointer to this character and replace it with a '\ 0' characters.
/* * strrchr example code * http://code-reference.com/c/string.h/strrchr */ #include <stdio.h> #include <string.h> int main( void ) { char string[]="The answer might 42, if the Earth v 1.0 not exist anymore"; char *ptr; ptr = strrchr (string, ','); if (ptr != NULL) { *ptr = '\0'; } printf ("%s\n", string); return 0; }
The answer might 42