summaryrefslogtreecommitdiff
path: root/ft_strrchr.c
blob: 17dfcb50099a035a1897b7a2367759e55938ddeb (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Libft Copyright (C) 2026 yctct

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "libft.h"

char	*ft_strrchr(const char *s, int c)
{
	int	slen;

	slen = ft_strlen(s);
	while (slen >= 0)
	{
		if (s[slen] == (char)c)
			return ((char *)&s[slen]);
		slen--;
	}
	return (0);
}

/*
char	*ft_strrchr(const char *p, int ch)
{
	int	i;

	i = 0;
	while (s[i])
		i++;
	while (s[i] != (char)c)
		if (i-- == 0)
			return (NULL);
	return ((char *)&s[i]);
}

char	*ft_strrchr(const char *p, int ch)
{
	char	*save;

	save = NULL;
	while (1)
	{
		if (*p == (char)ch)
			save = (char *)p;
		if (!*p++)
			return (save);
	}
}

#include <string.h>

int	main(void)
{
	const char	s[] = "Helleo";
	int			i;
	int			j;
	int			k;

	i = (int) 'i';
	j = (int) 'e';
	k = '\0';
	printf("Expected return: NULL\n");
	printf("ft_strrchr: %s\n", ft_strrchr(s, i));
	printf("   strrchr: %s\n", strrchr(s, i));
	printf("------------------------\n");
	printf("Expected return: 'eo'\n");
	printf("ft_strrchr: %s\n", ft_strrchr(s, j));
	printf("   strrchr: %s\n", strrchr(s, j));
	printf("------------------------\n");
	printf("Expected return: 'null byte'\n");
	printf("ft_strrchr: %s\n", ft_strrchr(s, k));
	printf("   strrchr: %s\n", strrchr(s, k));
}
*/