piscine-c07/ex04/ft_convert_base2.c

73 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 22:43:16 by tosuman #+# #+# */
/* Updated: 2023/03/30 22:43:17 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strcat(char *dest, char *src)
{
char *orig_s1;
orig_s1 = dest;
while (*dest)
++dest;
while (*src)
*dest++ = *src++;
*dest = 0;
return (orig_s1);
}
unsigned int char_count(const char *str, char c)
{
unsigned int count;
count = 0;
while (*str)
{
if (*str == c)
++count;
++str;
}
return (count);
}
// Return 0 if the base is invalid or the length of the base otherwise
unsigned int is_valid_base(char *base, int space_ok)
{
unsigned int size;
size = 0;
while (*base)
{
if (*base == '+' || *base == '-' || (*base == ' ' && !space_ok))
return (0);
if (char_count(base, *base) > 1)
return (0);
++size;
++base;
}
if (size < 2)
return (0);
return (size);
}
int char_index(const char *str, const char c)
{
int idx;
idx = 0;
while (*str)
{
if (*str++ == c)
return (idx);
++idx;
}
return (-1);
}