User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:string.h:strrchr

strrchr

#include <stdlib.h>
char *strrchr(const char *str, int c);

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

/* 
 * 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;
}

output of strrchr c example

  The answer might 42
       

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/string.h/strrchr.txt · Last modified: 2024/02/16 01:06 (external edit)

Impressum Datenschutz