Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- void usage()
- {
- printf("Provide string number(only 0-9 and '-' sign) to parse into integer!\n");
- }
- int toInt(const char c) {
- if(c > '9' || c < '0')
- return -1;
- return c - '0';
- }
- int main(int argc, char** argv)
- {
- if (argc < 2) {
- usage();
- return -1;
- }
- bool minus = argv[1][0] == '-';
- printf("Minus: %s\n", minus == true ? "true" : "false");
- int number = 0;
- int len = strlen(argv[1]);
- printf("Len %d\n", len);
- for (int i = minus ? 1 : 0; i < len; i++) {
- int num = toInt(argv[1][i]);
- if (num == -1) {
- usage();
- return -2;
- }
- number = number * 10 + num;
- }
- if (minus) {
- number *= -1;
- }
- printf("input: %s, numer: %d\n", argv[1], number);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement