-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-strchr.c
40 lines (35 loc) · 858 Bytes
/
2-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
#include "main.h"
#include <stddef.h>
#include <stdio.h>
/**
* _strchr - locates a character in a string
* @s: pointer to string to locate character
* @c: character to locate
*
* Return: pointer to character found
* NULL if the character is not found
*
*
* We used a loop to find the size of s.
* so that we use `len` in the 2nd loop instead of `s[i] != '\0'`
* the loop gives us the number of elements,
* hence if elements are 5, the last element is on the 4th index.
* but we iterate up to the 5th index, so that we include the null character
* `c` might be a null character, and if so, we still print it.
*/
char *_strchr(char *s, char c)
{
int i, len;
char *found = NULL;
for (len = 0; s[len] != '\0'; len++)
;
for (i = 0; i <= len; i++)
{
if (s[i] == c)
{
found = &s[i];
return (found);
}
}
return (NULL);
}