-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strrchr.c
54 lines (48 loc) · 1.92 KB
/
ft_strrchr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbrito-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 20:46:47 by mbrito-p #+# #+# */
/* Updated: 2023/04/26 20:46:47 by mbrito-p ### ########.fr */
/* */
/* ************************************************************************** */
// The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = 0;
// to find the lenght of the string
while (s[i])
i++;
//starting from the last position check for the character
while (i >= 0)
{
if (s[i] == (char)c)
//s + i to return the position of the last found
// c character on the string. s is the first position, i
// is the int number representing the position where
// th character was found
return ((char *)(s + i));
i--;
}
return (0);
}
// int main(void)
// {
// const char *str = "Hello, world!";
// char c = 'o';
// char *result = ft_strrchr(str, c);
// if (result != NULL)
// {
// printf("'%c' was found at position %ld in the string '%s'\n", c, result - str, str);
// }
// else
// {
// printf("'%c' was not found in the string '%s'\n", c, str);
// }
// return 0;
// }