maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
ft_strnstr.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_strnstr.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rmikhayl <rmikhayl@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/12/17 17:25:27 by rmikhayl #+# #+# */
9/* Updated: 2023/12/17 17:25:27 by rmikhayl ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14
15/* *************************** ft_strnstr *********************************** */
16/* Searches for the first occurrence of 'little' within 'big,' up to 'n' */
17/* characters. Returns a pointer to the found substring or NULL if not */
18/* found. */
19/* */
20/* In layman's terms: It's like looking for a specific word or phrase in a */
21/* longer text, but you can only search within the first 'n' characters. If */
22/* it's there, you get a pointer to it; otherwise, you get nothing. */
23/* ************************************************************************** */
24
25char *ft_strnstr(const char *big, const char *little, size_t n)
26{
27 size_t little_len;
28
29 if (!big && n == 0)
30 return (NULL);
31 little_len = ft_strlen(little);
32 if (little[0] == '\0')
33 return ((char *)big);
34 while (*big != '\0' && n >= little_len)
35 {
36 if (ft_strncmp(big, little, little_len) == 0)
37 return ((char *)big);
38 big++;
39 n--;
40 }
41 return (NULL);
42}
char * ft_strnstr(const char *big, const char *little, size_t n)
Definition ft_strnstr.c:25
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition ft_strncmp.c:24
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15