summaryrefslogtreecommitdiff
path: root/ft_substr.c
blob: f5ee2aa67bbc21c53fe6a22e39a01f1df71c8447 (plain)
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
41
42
43
44
45
46
47
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_substr.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/03/06 21:58:31 by dkaiser           #+#    #+#             */
/*   Updated: 2024/03/07 14:55:55 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>

char	*ft_substr(char const *s, unsigned int start, size_t len)
{
	size_t	i;
	char	*result;

	i = 0;
	while (s[i])
		i++;
	if (i - start < len)
		len = (i - start);
	if (start >= len)
		len = 0;
	result = malloc(len + 1);
	if (result)
	{
		result[len] = '\0';
		i = 0;
		while (i < len)
		{
			result[i] = s[i + start];
			i++;
		}
	}
	return (result);
}

/* #include <stdio.h> */
/* int main () */
/* { */
/*     char s[] = "Hello there"; */
/*     char *substr = ft_substr(s, 25, 10); */
/*     printf("%s\n", substr); */
/* } */