strrchr, wcsrchr (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Scan a string for the last occurrence of a character.

char *strrchr( const char *string, int c );wchar *wcsrchr( const wchar_t *string, intc );

Parameters

  • string
    Null-terminated string to search.
  • c
    Character to be located.

Return Values

Each of these functions returns a pointer to the last occurrence of c in string, or NULL if c is not found.

Remarks

These functions are supported by all versions of the C run-time libraries.

The strrchr function finds the last occurrence of c (converted to char) in string. The search includes the terminating null character.

wcsrchr is a wide-character version of strrchr. The arguments and return value of wcsrchr are wide-character strings. These two functions behave identically otherwise.

The arguments and return value of wcsrchr are wide-character strings.

The following table shows generic-text routine mappings for this function.

TCHAR.H Routine _UNICODE Defined
_tcsrchr wcsrchr

For more information about TCHAR.H routines, see Generic Text Mappings.

Example

Description

This program illustrates searching for a character with strchr (search forward) or strrchr (search backward).

Code

#include <string.h>
#include <stdio.h>

int  ch = 'r';

char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

void main( void )
{
   char *pdest;
   int result;

   printf( "String to be searched: \n\t\t%s\n", string );
   printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
   printf( "Search char:\t%c\n", ch );

   /* Search forward. */
   pdest = strchr( string, ch );
   result = pdest - string + 1;
   if( pdest != NULL )
      printf( "Result:\tfirst %c found at position %d\n\n", 
              ch, result );
   else
      printf( "Result:\t%c not found\n" );

   /* Search backward. */
   pdest = strrchr( string, ch );
   result = pdest - string + 1;
   if( pdest != NULL )
      printf( "Result:\tlast %c found at position %d\n\n", ch, result );
   else
      printf( "Result:\t%c not found\n" );
}
// Output
String to be searched: 
      The quick brown dog jumps over the lazy fox
               1         2         3         4         5
      12345678901234567890123456789012345678901234567890

Search char:   r
Result:   first r found at position 12

Result:   last r found at position 30

Requirements

OS Versions: Windows CE 2.0 and later.

Header: stdio.h, string.h.

Link Library: coredll.dll.

See Also

strchr | strcspn

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.