-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strchr.c
47 lines (43 loc) · 1.66 KB
/
ft_strchr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibeliaie <ibeliaie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 17:44:38 by ibeliaie #+# #+# */
/* Updated: 2023/05/24 16:29:07 by ibeliaie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* locate first occurrence of character in string */
char *ft_strchr(const char *str, int c)
{
int i;
unsigned char target;
i = 0;
target = (unsigned char)c;
while (str[i])
{
if (str[i] == target)
return ((char *)str + i);
i++;
}
if (target == '\0')
return ((char *)str + i);
return (0);
}
// int main()
// {
// const char *str = "A noisy noise annoys an oyster most.";
// int target = 'y';
// char *result = ft_strchr(str, target);
// if (result != NULL)
// printf("'%c' is found at position: %ld\n", target, result - str);
// else
// printf("'%c' was not found in the string.\n", target);
// return 0;
// }
// //
//assigning unsigned char to c allows for proper comparison
//even if c is outside the range of char value