From ff4846af7dea497d75af09acfa6c2c8ca48fd67e Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 18 Jun 2022 16:13:44 +0530 Subject: 5.6 --- 5.6.5.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 5.6.5.c (limited to '5.6.5.c') diff --git a/5.6.5.c b/5.6.5.c new file mode 100644 index 0000000..d04da1e --- /dev/null +++ b/5.6.5.c @@ -0,0 +1,47 @@ +#include +#include + +#define MAXLEN 1000 + +int atoi(char str[]); +int sneed_getline(char str[], int max); + +int +main(void) +{ + char str[MAXLEN]; + while (sneed_getline(str, MAXLEN)) { + printf("string ver : %s", str); + printf("int ver : %d\n", atoi(str)); + } + return 0; +} + +int +atoi(char str[]) +{ + int val; + + for (val = 0; isdigit(*str) && *str; str++) + val = val * 10 + *str -'0'; + + return val; +} + +int +sneed_getline(char str[], int max) +{ + char input; + char *str_og = str; + + while (--max && (*str = input = getchar()) != EOF && input != '\n') + str++; + + if (!max && input != '\n') + *str = '\n', str++; + + str = '\0'; + + return str - str_og -1; +} + -- cgit v1.2.3