Skip to content

Commit

Permalink
add function ft_strchr
Browse files Browse the repository at this point in the history
  • Loading branch information
Marilia Brito Passos committed Apr 17, 2023
1 parent 3a25d0f commit 05b0219
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NAME = libft.a
SRC = ft_strlen.c ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c ft_memset.c\
ft_bzero.c ft_memcpy.c ft_memmove.c ft_toupper.c ft_tolower.c ft_strlcpy.c ft_strlcat.c
ft_bzero.c ft_memcpy.c ft_memmove.c ft_toupper.c ft_tolower.c ft_strlcpy.c ft_strlcat.c\
ft_strchr.c
OBJS = ${SRC:.c=.o}
HEADER = include/libft.h
INCLUDE = -I include
Expand Down
35 changes: 35 additions & 0 deletions ft_strchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbrito-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/17 20:33:14 by mbrito-p #+# #+# */
/* Updated: 2023/04/17 20:41:01 by mbrito-p ### ########.fr */
/* */
/* ************************************************************************** */

// The strchr function in C is used to search a given string
// (i.e., a sequence of characters) for the first occurrence
// of a specified character.The str parameter is a pointer to the string
// in which the search is to be performed.
// The c parameter is the character that you want to search for in the string.
// The function returns a pointer to the first occurrence
// of the character c in the string str. If the character
// is not found, the function returns NULL.
#include "libft.h"

char *ft_strchr(const char *s, int c)
{
while (*s != '\0') {
if (*s == c) {
return (char *)s;
}
s++;
}
if (*s == c) {
return (char *)s;
}
return NULL;
}
1 change: 1 addition & 0 deletions include/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ int ft_toupper(int c);
int ft_tolower(int c);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strchr(const char *s, int c);
#endif
Binary file modified libft.a
Binary file not shown.
23 changes: 11 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
/* By: mbrito-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/12 20:20:59 by mbrito-p #+# #+# */
/* Updated: 2023/04/14 20:57:10 by mbrito-p ### ########.fr */
/* Updated: 2023/04/17 20:41:41 by mbrito-p ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int main(void)
{
char str1[20] = "Hello, ";
char *str2 = "world!";
size_t dest_size = sizeof(str1);
size_t result = ft_strlcat(str1, str2, dest_size);
printf("Result: %zu, Destination string: %s\n", result, str1);

return 0;
}

int main() {
char str[] = "hello world";
char *ptr = ft_strchr(str, 'w');
if (ptr != NULL) {
printf("The character 'w' was found in the string at position %ld.\n", ptr - str);
} else {
printf("The character 'w' was not found in the string.\n");
}
return 0;
}
Binary file modified teste
Binary file not shown.

0 comments on commit 05b0219

Please sign in to comment.