Browse Source

[enh] libft: add function which search pos of a char in a string.

Moul 4 years ago
parent
commit
5e1c546d47
3 changed files with 35 additions and 2 deletions
  1. 2 1
      Makefile
  2. 2 1
      libft.h
  3. 31 0
      src/ft_search_pos_char.c

+ 2 - 1
Makefile

@@ -6,7 +6,7 @@
 #    By: mazimi <mazimi@student.42.fr>              +#+  +:+       +#+         #
 #                                                 +#+#+#+#+#+   +#+            #
 #    Created: 2014/11/05 11:09:24 by mazimi            #+#    #+#              #
-#    Updated: 2016/03/25 14:38:53 by mazimi           ###   ########.fr        #
+#    Updated: 2016/10/08 11:28:26 by mazimi           ###   ########.fr        #
 #                                                                              #
 # **************************************************************************** #
 
@@ -29,6 +29,7 @@ ft_strcat.c \
 ft_strncat.c \
 ft_strlcat.c \
 ft_strchr.c \
+ft_search_pos_char.c \
 ft_strrchr.c \
 ft_strstr.c \
 ft_strnstr.c \

+ 2 - 1
libft.h

@@ -6,7 +6,7 @@
 /*   By: mazimi <mazimi@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2014/11/23 19:50:04 by mazimi            #+#    #+#             */
-/*   Updated: 2016/03/25 14:39:11 by mazimi           ###   ########.fr       */
+/*   Updated: 2016/10/08 11:28:44 by mazimi           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -44,6 +44,7 @@ char				*ft_strcat(char *s1, const char *s2);
 char				*ft_strncat(char *s1, const char *s2, size_t n);
 size_t				ft_strlcat(char *dst, const char *src, size_t size);
 char				*ft_strchr(const char *s, int c);
+int					ft_search_pos_char(const char *s1, char c);
 char				*ft_strrchr(const char *s, int c);
 char				*ft_strstr(const char *s1, const char *s2);
 char				*ft_strnstr(const char *s1, const char *s2, size_t n);

+ 31 - 0
src/ft_search_pos_char.c

@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_search_pos_char.c                               :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: mazimi <marvin@42.fr>                      +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2016/10/12 18:07:23 by mazimi            #+#    #+#             */
+/*   Updated: 2016/10/12 18:07:25 by mazimi           ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../libft.h"
+
+/*
+** Return string's position of first occurence of searched char
+** If char not found, return '-1'
+*/
+
+int		ft_search_pos_char(const char *s1, char c)
+{
+	int		i;
+
+	i = -1;
+	while (s1[++i] != '\0')
+		if (s1[i] == c)
+			return (i);
+	if (s1[i] == c)
+		return (i);
+	return (-1);
+}