-
Notifications
You must be signed in to change notification settings - Fork 0
strlen
Marcos edited this page Oct 1, 2024
·
1 revision
La función ft_strlen calcula la longitud de una cadena de caracteres, excluyendo el carácter nulo ('\0') que marca el final de la cadena.
STRLEN(3) (simplified)
NAME
strlen -- find length of string
SYNOPSIS
size_t(const char *s);
DESCRIPTION
The strlen() function computes the length of the string s.
RETURN VALUES
The strlen() function returns the number of characters that precede the terminating NUL character.
La función strlen() recorre la cadena de caracteres pasada como argumento y cuenta el número de caracteres hasta llegar al carácter nulo ('\0'), que indica el final de la cadena. El valor devuelto es el número de caracteres, sin incluir el carácter nulo.
Aquí te muestro mi implementación de la función ft_strlen en mi proyecto de Libft.
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}El siguiente código es un ejemplo de cómo puedes probar la función ft_strlen():
#include <stdio.h>
size_t ft_strlen(const char *s);
int main(void)
{
char str1[] = "Hello, world!";
char str2[] = "";
printf("Longitud de 'Hello, world!': %zu\n", ft_strlen(str1)); // Debe devolver 13
printf("Longitud de una cadena vacía: %zu\n", ft_strlen(str2)); // Debe devolver 0
return 0;
}