Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/strrchr"

From cppreference.com
< cpp‎ | string‎ | byte
m (r2.7.3) (Robot: Adding de, es, it, pt, ru)
m (langlinks)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/title| strrchr}}
+
{{cpp/title|strrchr}}
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | cstring}}
+
{{dcl header|cstring}}
{{ddcl list item |
+
{{dcl|
const char *strrchr( const char *str, int ch );
+
const char* strrchr( const char* str, int ch );
 
}}
 
}}
{{ddcl list item | 1=
+
{{dcl|1=
     char *strrchr(      char *str, int ch );
+
     char* strrchr(      char* str, int ch );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Finds the last occurrence of the character {{tt|ch}} in the byte string pointed to by {{tt|str}}.  
+
Finds the last occurrence of {{c|ch}} (after conversion to {{c|char}}) in the byte string pointed to by {{c|str}}. The terminating null character is considered to be a part of the string and can be found if searching for {{c|'\0'}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | str | pointer to the null-terminated byte string to be analyzed}}
+
{{par|str|pointer to the null-terminated byte string to be analyzed}}
{{param list item | ch | character to search for}}
+
{{par|ch|character to search for}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
Pointer to the found character in {{tt|str}}, or {{c|NULL}} if no such character is found.
+
Pointer to the found character in {{c|str}}, or null pointer if no such character is found.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cstring>
| output=
+
#include <iostream>
 +
 
 +
int main()
 +
{
 +
    char input[] = "/home/user/hello.c";
 +
    char* output = std::strrchr(input, '/');
 +
    if (output)
 +
        std::cout << output + 1 << '\n';
 +
}
 +
|output=
 +
hello.c
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/byte/dcl list strchr}}
+
{{dsc inc|cpp/string/byte/dsc strchr}}
{{dcl list template | cpp/string/byte/dcl list strpbrk}}
+
{{dsc inc|cpp/string/wide/dsc wcsrchr}}
{{dcl list see c | c/string/byte/strrchr}}
+
{{dsc inc|cpp/string/basic_string/dsc rfind}}
{{dcl list end}}
+
{{dsc see c|c/string/byte/strrchr}}
 +
{{dsc end}}
  
[[de:cpp/string/byte/strrchr]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/byte/strrchr]]
+
[[fr:cpp/string/byte/strrchr]]
+
[[it:cpp/string/byte/strrchr]]
+
[[ja:cpp/string/byte/strrchr]]
+
[[pt:cpp/string/byte/strrchr]]
+
[[ru:cpp/string/byte/strrchr]]
+
[[zh:cpp/string/byte/strrchr]]
+

Latest revision as of 14:41, 6 June 2023

Defined in header <cstring>
const char* strrchr( const char* str, int ch );
      char* strrchr(       char* str, int ch );

Finds the last occurrence of ch (after conversion to char) in the byte string pointed to by str. The terminating null character is considered to be a part of the string and can be found if searching for '\0'.

Contents

[edit] Parameters

str - pointer to the null-terminated byte string to be analyzed
ch - character to search for

[edit] Return value

Pointer to the found character in str, or null pointer if no such character is found.

[edit] Example

#include <cstring>
#include <iostream>
 
int main()
{
    char input[] = "/home/user/hello.c";
    char* output = std::strrchr(input, '/');
    if (output)
        std::cout << output + 1 << '\n';
}

Output:

hello.c

[edit] See also

finds the first occurrence of a character
(function) [edit]
finds the last occurrence of a wide character in a wide string
(function) [edit]
find the last occurrence of a substring
(public member function of std::basic_string<CharT,Traits,Allocator>) [edit]
C documentation for strrchr