This shows you the differences between two versions of the page.
|
c:string.h:strrchr [2012/07/16 22:41] 127.0.0.1 external edit |
c:string.h:strrchr [2024/02/16 01:06] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | char *strrchr(const char *str, int c); | + | ===== strrchr ===== |
| + | |||
| + | <code c> | ||
| + | #include <stdlib.h> | ||
| + | char *strrchr(const char *str, int c); | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== description of strrchr ===== | ||
| + | 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 C Sourcecode Example ===== | ||
| + | <code c> | ||
| + | /* | ||
| + | * 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; | ||
| + | } | ||
| + | |||
| + | </code> | ||
| + | |||
| + | ==== output of strrchr c example ==== | ||
| + | The answer might 42 | ||
| + | |||