From 32c725603f912448a6ac12a9405d4c2e002304c0 Mon Sep 17 00:00:00 2001 From: Mohammed YMIK Date: Fri, 12 Jun 2020 14:56:57 +0100 Subject: [PATCH 1/5] add atoi that convert string to integer --- conversions/c_atoi_str_to_integer.c | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 conversions/c_atoi_str_to_integer.c diff --git a/conversions/c_atoi_str_to_integer.c b/conversions/c_atoi_str_to_integer.c new file mode 100644 index 00000000..ac9cc81e --- /dev/null +++ b/conversions/c_atoi_str_to_integer.c @@ -0,0 +1,40 @@ +#include + +// recoding the original atoi function in stdlib.h +int c_atoi(char *str) +{ + int i; + int sign; + long value; + long prev; + + i = 0; + sign = 1; + value = 0; + // skip wait spaces + while (((str[i] <= 13 && str[i] >= 9) || str[i] == 32) && str[i] != '\0') + i++; + // store the sign + if (str[i] == '-' || str[i] == '+') + (str[i++] == '-') ? sign = -1 : 1; + // convert char by char to an int value + while (str[i] >= 48 && str[i] <= 57 && str[i] != '\0') + { + prev = value; + value = value * 10 + sign * (str[i] - '0'); + // manage the overflow + if (sign == 1 && prev > value) + return (-1); + else if (sign == -1 && prev < value) + return (0); + i++; + } + return (value); +} + + +int main() +{ + // Test + printf("%d\n", c_atoi("-234")); +} From 18c677d6a5f8423e90497f7bef566841fe28359b Mon Sep 17 00:00:00 2001 From: Mohammed YMIK Date: Fri, 12 Jun 2020 16:11:41 +0100 Subject: [PATCH 2/5] Add doc, signature, indentation --- conversions/c_atoi_str_to_integer.c | 109 ++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/conversions/c_atoi_str_to_integer.c b/conversions/c_atoi_str_to_integer.c index ac9cc81e..3d213c24 100644 --- a/conversions/c_atoi_str_to_integer.c +++ b/conversions/c_atoi_str_to_integer.c @@ -1,40 +1,87 @@ +/** + * \file + * \brief Recoding the original atoi function in stdlib.h + * \author [Krishna Vedala](https://github.com/kvedala) + * + * The function convert a string passed to an integer + */ #include +#include +#include +#include -// recoding the original atoi function in stdlib.h -int c_atoi(char *str) +/** + * the function take a string and return an integer + * \param[out] str pointer to a char address + */ +int c_atoi(const char *str) { - int i; - int sign; - long value; - long prev; + /** + * i is a counter + * sign hold the sign (negative or positive) + * value hold the current value by default set to 0 + * prev hold the previous value for managing the overflow + */ + int i; + int sign; + long value; + long prev; - i = 0; - sign = 1; - value = 0; - // skip wait spaces - while (((str[i] <= 13 && str[i] >= 9) || str[i] == 32) && str[i] != '\0') - i++; - // store the sign - if (str[i] == '-' || str[i] == '+') - (str[i++] == '-') ? sign = -1 : 1; - // convert char by char to an int value - while (str[i] >= 48 && str[i] <= 57 && str[i] != '\0') - { - prev = value; - value = value * 10 + sign * (str[i] - '0'); - // manage the overflow - if (sign == 1 && prev > value) - return (-1); - else if (sign == -1 && prev < value) - return (0); - i++; - } - return (value); + i = 0; + sign = 1; + value = 0; + + /* skipping the spaces */ + while (((str[i] <= 13 && str[i] >= 9) || str[i] == 32) && str[i] != '\0') + i++; + + /* store the sign if it is negative sign */ + if (str[i] == '-' || str[i] == '+') + (str[i++] == '-') ? sign = -1 : 1; + + /* converting char by char to a numeric value */ + while (str[i] >= 48 && str[i] <= 57 && str[i] != '\0') + { + prev = value; + value = value * 10 + sign * (str[i] - '0'); + + /* managing the overflow */ + if (sign == 1 && prev > value) + return (-1); + else if (sign == -1 && prev < value) + return (0); + i++; + } + return (value); +} + +/** + * test the function implementation + */ +int test_c_atoi() +{ + printf("<<<< TEST FUNCTION >>>>\n"); + assert(c_atoi("123") == atoi("123")); + assert(c_atoi("-123") == atoi("-123")); + assert(c_atoi("") == atoi("")); + assert(c_atoi("-h23") == atoi("-h23")); + assert(c_atoi(" 23") == atoi(" 23")); + assert(c_atoi("999999999999") == atoi("999999999999")); + printf("<<<< TEST DONE >>>>\n"); } -int main() +/** + * the main function take one argument of type char* + * example : ./program 123 + */ +int main(int argc, char **argv) { - // Test - printf("%d\n", c_atoi("-234")); + if (argc == 2) + { + printf("Your number + 5 is %d\n", c_atoi(argv[1]) + 5); + return (0); + } + printf("wrong number of parmeters\n"); + return (1); } From f0b872e979417f729d986ac0e4cdedb6cc6655d4 Mon Sep 17 00:00:00 2001 From: Mohammed YMIK <35063009+medymik@users.noreply.github.com> Date: Fri, 12 Jun 2020 16:13:32 +0100 Subject: [PATCH 3/5] Update author --- conversions/c_atoi_str_to_integer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/c_atoi_str_to_integer.c b/conversions/c_atoi_str_to_integer.c index 3d213c24..fa63f0fb 100644 --- a/conversions/c_atoi_str_to_integer.c +++ b/conversions/c_atoi_str_to_integer.c @@ -1,7 +1,7 @@ /** * \file * \brief Recoding the original atoi function in stdlib.h - * \author [Krishna Vedala](https://github.com/kvedala) + * \author [Medymik](https://github.com/medymik) * * The function convert a string passed to an integer */ From ddc689a335bf79c9e0b765417def4ca149c3ccea Mon Sep 17 00:00:00 2001 From: Mohammed YMIK Date: Fri, 12 Jun 2020 16:38:52 +0100 Subject: [PATCH 4/5] Done --- conversions/c_atoi_str_to_integer.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/conversions/c_atoi_str_to_integer.c b/conversions/c_atoi_str_to_integer.c index 3d213c24..01ba2c53 100644 --- a/conversions/c_atoi_str_to_integer.c +++ b/conversions/c_atoi_str_to_integer.c @@ -1,7 +1,7 @@ /** * \file * \brief Recoding the original atoi function in stdlib.h - * \author [Krishna Vedala](https://github.com/kvedala) + * \author [Mohammed YMIK](https://github.com/medymik) * * The function convert a string passed to an integer */ @@ -16,12 +16,6 @@ */ int c_atoi(const char *str) { - /** - * i is a counter - * sign hold the sign (negative or positive) - * value hold the current value by default set to 0 - * prev hold the previous value for managing the overflow - */ int i; int sign; long value; From 5cc83fe0368987576d84e94953593cc75ef85a9c Mon Sep 17 00:00:00 2001 From: Mohammed YMIK Date: Fri, 12 Jun 2020 16:53:37 +0100 Subject: [PATCH 5/5] up to date --- conversions/c_atoi_str_to_integer.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/conversions/c_atoi_str_to_integer.c b/conversions/c_atoi_str_to_integer.c index 6d491f5a..e16c7774 100644 --- a/conversions/c_atoi_str_to_integer.c +++ b/conversions/c_atoi_str_to_integer.c @@ -1,12 +1,7 @@ /** * \file * \brief Recoding the original atoi function in stdlib.h -<<<<<<< HEAD - * \author [Mohammed YMIK](https://github.com/medymik) -======= - * \author [Medymik](https://github.com/medymik) ->>>>>>> f0b872e979417f729d986ac0e4cdedb6cc6655d4 - * + * \author [Mohammed YMIK](https://github.com/medymik)W * The function convert a string passed to an integer */ #include