Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | byte
m (Shorten template names. Use {{lc}} where appropriate.)
m (langlinks)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{cpp/title| strrchr}}
+
{{cpp/title|strrchr}}
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cstring}}
+
{{dcl header|cstring}}
{{dcl |
+
{{dcl|
 
const char* strrchr( const char* str, int ch );
 
const char* strrchr( const char* str, int ch );
 
}}
 
}}
{{dcl | 1=
+
{{dcl|1=
 
     char* strrchr(      char* str, int ch );
 
     char* strrchr(      char* str, int ch );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Finds the last occurrence of {{tt|ch}} (after conversion to {{c|char}}) in the byte string pointed to by {{tt|str}}. The terminating null character is considered to be a part of the string and can be found if searching for {{c|'\0'}}.
+
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===
 
{{par begin}}
 
{{par begin}}
{{par | str | pointer to the null-terminated byte string to be analyzed}}
+
{{par|str|pointer to the null-terminated byte string to be analyzed}}
{{par | ch | character to search for}}
+
{{par|ch|character to search for}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Pointer to the found character in {{tt|str}}, or null pointer 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 <iostream>
+
 
#include <cstring>
 
#include <cstring>
 +
#include <iostream>
  
 
int main()
 
int main()
Line 33: Line 32:
 
     char input[] = "/home/user/hello.c";
 
     char input[] = "/home/user/hello.c";
 
     char* output = std::strrchr(input, '/');
 
     char* output = std::strrchr(input, '/');
     if(output)
+
     if (output)
         std::cout << output+1 << '\n';
+
         std::cout << output + 1 << '\n';
 
}
 
}
| output=
+
|output=
 
hello.c
 
hello.c
 
}}
 
}}
Line 42: Line 41:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/byte/dcl list strchr}}
+
{{dsc inc|cpp/string/byte/dsc strchr}}
{{dsc inc | cpp/string/wide/dcl list wcsrchr}}
+
{{dsc inc|cpp/string/wide/dsc wcsrchr}}
{{dsc inc | cpp/string/basic_string/dcl list rfind}}
+
{{dsc inc|cpp/string/basic_string/dsc rfind}}
{{dsc see c | c/string/byte/strrchr}}
+
{{dsc see c|c/string/byte/strrchr}}
 
{{dsc end}}
 
{{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