Questions tagged [strrchr]

25 questions
7
votes
3 answers

Understanding strrchr function

I'm doing some tests with function strrchr, but I can't understand the output: $text = 'This is my code'; echo strrchr($text, 'my'); //my code Ok, the function returned the string before last occurrence $text = 'This is a test to test code'; echo…
viniciuswebdev
  • 1,286
  • 1
  • 11
  • 20
7
votes
5 answers

Why strrchr() returns `char*` instead of `const char*`?

The function char* strrchr(const char *str, int ch) returns a pointer (char*) within str (const char *) where the last occurrence of ch is located. So we can write the following code without any cast: #include int main() { const char…
oHo
  • 51,447
  • 27
  • 165
  • 200
5
votes
3 answers

Most efficient way of finding last char occurrence

Consider this function I wrote real quick to find the last occurrence of a given char in a string and return it's position within the array of chars that physically is the string: size_t strlstchar(const char *str, const char ch) { char *chptr =…
Keith Miller
  • 1,337
  • 1
  • 16
  • 32
3
votes
2 answers

Cut string by substrings after none left

I have a string Field-Text-Datepicker. I need to "explode" it to following array: array( [0] => field-text-datepicker [1] => field-text [2] => field ); I've tried some combinations of strrchr(), recursion and for loops, but everything…
Petr Cibulka
  • 2,452
  • 4
  • 28
  • 45
3
votes
2 answers

STRCHR vs STRRCHR difference?

I would like to know the difference between the two different uses. I believe the difference in some what very subtle. This is an explanation taken from the IBM reference manual. However maybe my english is bad, I just can't visualize the…
hayonj
  • 1,429
  • 3
  • 21
  • 28
2
votes
2 answers

How to extract the leftover substring from strrchr() or strchr()?

Let's say I have the following string which looks like a file path: char *filepath = "this/is/my/path/to/file.jpg"; I can use strrchr() to extract file.jpg as follows: char *filename = strrchr(filepath, '/') + 1; Now I need to extract and store…
blackbeard
  • 385
  • 2
  • 12
2
votes
3 answers

strrchr with strtr or str_replace in PHP

I have searched a lot of sites and SO answers for replacing characters in strings, but I didn't find a solution. I need to replace only the last used character 'é' of a string. It should work for every string, also if the string only contains once…
Grischa
  • 70
  • 8
  • 26
2
votes
4 answers

Strange behavior with function strrchr

I get some strange result with strrchr. please help me. Here is my code: #include #include #include int main() { char szString[32] = "hello-world-and-everyone"; char* ptemp = NULL; ptemp = strrchr(szString,…
1
vote
3 answers

segmentation fault in use of strrchr function

Here is my program which reverses a string. I am trying to use function strrchr at the end because I am entering the string James Bond on standard input. So I want to see if strrchr function gives a pointer to value character B of the above…
Registered User
  • 5,173
  • 16
  • 47
  • 73
1
vote
1 answer

Unable to execute a code that finds first character of a string that matches with a given character

This code is written to identify the position of character in the string from back which matches first with a given character.When i use scanf to get string,the compiler doesn't ask for the character and directly gives the output as 0.I am unable to…
1
vote
1 answer

PHP return character after value in string

I have a dynamic string that looks like this... /tester?bc=7&tester=orange Using PHP I am trying to return the single value after bc= I have looked at strrchr but that doesn't seem to let me specify just returning one character, how should I be…
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
1
vote
1 answer

Why does GCC accept convertion from 'const char *' to 'char *' on std::strrchr() returned value?

While adding a detailed answer, I noticed that GCC does not warn the following code while Visual C++ complains. #include int main() { const char CONSTSTR[] = "foo/bar/foobar.txt"; char *nonconst = std::strrchr (CONSTSTR, '/'); …
oHo
  • 51,447
  • 27
  • 165
  • 200
0
votes
0 answers

Is there a caseinsensitive version of strrchr and wcsrchr?(c++)

I try to check if variable ends with expression. My current code is this: (strchr(variable.data(), tolower(expression[0])) || strchr(variable.data(), toupper(expression[0]))) && _stricmp((strrchr(variable.data(), tolower(expression[0]))?…
ojesseus1
  • 43
  • 5
0
votes
0 answers

why do i get Segmentation fault (core dumped) error here?

while trying to regenerate the function strrchr i get this error, can someone help me understand this part? #include char *ft_strrchr(const char *s, int c) { int i; char *ptr; ptr = NULL; i = 0; while (*s) { if (*(s +…
0
votes
3 answers

PHP strrchr parse int

I've got a problem. This is my PHP code : $extract = $query; $extractpoint = strrchr($extract, "."); So, $extract is a parse_url of my website address. Exemple : http://test.com?param.6 $extract = param.6 and $extractpoint = .6 BUT, I want a…
1
2