Browse Source

[enh] add count_char_str().

Moul 4 years ago
parent
commit
13210735a7
3 changed files with 35 additions and 3 deletions
  1. 3 2
      Makefile
  2. 2 1
      libft.h
  3. 30 0
      src/count_char_str.c

+ 3 - 2
Makefile

@@ -6,7 +6,7 @@
 #    By: mazimi <mazimi@student.42.fr>              +#+  +:+       +#+         #
 #                                                 +#+#+#+#+#+   +#+            #
 #    Created: 2014/11/05 11:09:24 by mazimi            #+#    #+#              #
-#    Updated: 2016/02/19 17:59:09 by mazimi           ###   ########.fr        #
+#    Updated: 2016/02/27 18:47:53 by mazimi           ###   ########.fr        #
 #                                                                              #
 # **************************************************************************** #
 
@@ -81,7 +81,8 @@ ft_lstadd.c \
 ft_lstiter.c \
 ft_lstmap.c \
 sort_int_tab.c \
-get_next_line.c
+get_next_line.c \
+count_char_str.c
 
 SRC_PATH = src
 

+ 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/02/12 17:59:51 by mazimi           ###   ########.fr       */
+/*   Updated: 2016/02/27 18:47:33 by mazimi           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -96,5 +96,6 @@ void				ft_lstadd(t_list **alst, t_list *new);
 void				ft_lstiter(t_list *lst, void (*f)(t_list *elem));
 t_list				*ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
 void				sort_int_tab(int *tab, unsigned int size);
+int					count_char_str(char *str, char c);
 
 #endif

+ 30 - 0
src/count_char_str.c

@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   count_char_str.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: mazimi <marvin@42.fr>                      +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2016/02/27 18:47:06 by mazimi            #+#    #+#             */
+/*   Updated: 2016/02/27 18:47:15 by mazimi           ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../libft.h"
+
+/*
+** Count number of character occurences in a string.
+*/
+
+int		count_char_str(char *str, char c)
+{
+	int		i;
+	int		occ;
+
+	occ = 0;
+	i = -1;
+	while (str[++i] != '\0')
+		if (str[i] == c)
+			occ++;
+	return (occ);
+}