Skip to content

Instantly share code, notes, and snippets.

@dlupaescu
Last active August 6, 2019 13:00
Show Gist options
  • Save dlupaescu/20b91ae11bba185640a9 to your computer and use it in GitHub Desktop.
Save dlupaescu/20b91ae11bba185640a9 to your computer and use it in GitHub Desktop.
Level: 2 | ft_atoi.c | print_bits.c
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/08/21 18:09:18 by exam #+# #+# */
/* Updated: 2015/08/21 18:39:30 by exam ### ########.fr */
/* */
/* ************************************************************************** */
int ft_atoi(const char *str)
{
int i;
int nbr;
int negative;
nbr = 0;
negative = 0;
i = 0;
while ((str[i] == '\n') || (str[i] == '\t') || (str[i] == '\v') ||
(str[i] == ' ') || (str[i] == '\f') || (str[i] == '\r'))
i++;
if (str[i] == '-')
negative = 1;
if (str[i] == '+' || str[i] == '-')
i++;
while (str[i] && (str[i] >= '0') && (str[i] <= '9'))
{
nbr *= 10;
nbr += (int)str[i] - '0';
i++;
}
if (negative == 1)
return (-nbr);
else
return (nbr);
}
/*
Assignment name : ft_atoi
Expected files : ft_atoi.c
Allowed functions: None
--------------------------------------------------------------------------------
Write a function that converts the string argument str to an integer (type int)
and returns it.
It works much like the standard atoi(const char *str) function, see the man.
Your function must be declared as follows:
int ft_atoi(const char *str);
*/
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_bits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/08/21 17:33:04 by exam #+# #+# */
/* Updated: 2015/08/21 18:02:01 by exam ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void print_bits(unsigned char octet)
{
int i;
char c;
i = 128;
while (i > 0)
{
if (octet < i)
{
c = '0';
i = i / 2;
write(1, &c, 1);
}
else
{
c = '1';
write(1, &c, 1);
octet = octet - i;
i = i / 2;
}
}
}
/*
Assignment name : print_bits
Expected files : print_bits.c
Allowed functions: write
--------------------------------------------------------------------------------
Scrieti o functie care primeste un byte si afiseaza byte-ul in binar FARA LINIE NOUA LA SFARSTI.
Functia trebuie declarata ca si mai jos:
void print_bits(unsigned char octet);
Exemplu, daca trimiteti 2 functiei print_bits, va afisa "00000010"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment