-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strrchr.c
66 lines (61 loc) · 2.23 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
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jopedro3 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 12:01:02 by jopedro3 #+# #+# */
/* Updated: 2023/10/04 14:46:15 by jopedro3 ### ########.fr */
/* */
/* ************************************************************************** */
/*
Function: ft_strrchr
Purpose: Locate the last occurrence
of a character (c) in a string (s).
How it works:
- Initializes a pointer (last) to NULL,
which will store the location of the found character.
- Iterates through the string (s).
- If the current character matches (c),
updates 'last' to point to the current character.
- Continues to check until the end of
the string to find the last occurrence.
- If (c) is found, returns a pointer to the last occurrence.
- If (c) is not found, returns NULL.
- Also checks for (c) being the null-terminator
and updates 'last' accordingly.
*/
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
char *last;
last = NULL;
while (*s)
{
if (*s == (char)c)
last = (char *)s;
s++;
}
if (*s == (char)c)
last = (char *)s;
return (last);
}
/*#include <stdio.h>
int main(void)
{
char *str = "Hello, World! Programming is fun!";
char c1 = 'o';
char c2 = 'z';
char *result1 = ft_strrchr(str, c1);
if (result1)
printf("Last occurrence of '%c': %s\n", c1, result1);
else
printf("Character '%c' not found.\n", c1);
char *result2 = ft_strrchr(str, c2);
if (result2)
printf("Last occurrence of '%c': %s\n", c2, result2);
else
printf("Character '%c' not found.\n", c2);
return 0;
}*/