{"id":23684077,"url":"https://github.com/lepharamramchiary/string-to-integer-atoi","last_synced_at":"2026-01-06T05:30:21.115Z","repository":{"id":156031593,"uuid":"599482923","full_name":"LepharamRamchiary/String-to-Integer-atoi","owner":"LepharamRamchiary","description":null,"archived":false,"fork":false,"pushed_at":"2023-02-09T08:34:26.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T20:33:25.849Z","etag":null,"topics":["cpp"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LepharamRamchiary.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-09T08:27:35.000Z","updated_at":"2023-05-03T07:35:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"7d2be3d5-e9ff-460d-b0a1-cfac885a83f4","html_url":"https://github.com/LepharamRamchiary/String-to-Integer-atoi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LepharamRamchiary%2FString-to-Integer-atoi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LepharamRamchiary%2FString-to-Integer-atoi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LepharamRamchiary%2FString-to-Integer-atoi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LepharamRamchiary%2FString-to-Integer-atoi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LepharamRamchiary","download_url":"https://codeload.github.com/LepharamRamchiary/String-to-Integer-atoi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239735870,"owners_count":19688355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp"],"created_at":"2024-12-29T20:33:05.313Z","updated_at":"2026-01-06T05:30:21.054Z","avatar_url":"https://github.com/LepharamRamchiary.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"**# String-to-Integer(atoi)**\n```\n**Question**:-\nImplement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).\n\nThe algorithm for myAtoi(string s) is as follows:\n\n-Read in and ignore any leading whitespace.\n-Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.\n-Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.\n-Convert these digits into an integer (i.e. \"123\" -\u003e 123, \"0032\" -\u003e 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).\n-If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.\n-Return the integer as the final result.\n\nNote:\n-Only the space character ' ' is considered a whitespace character.\n-Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.\n```\nExample 1:\n```\nInput: s = \"42\"\nOutput: 42\nExplanation: The underlined characters are what is read in, the caret is the current reader position.\nStep 1: \"42\" (no characters read because there is no leading whitespace)\n         ^\nStep 2: \"42\" (no characters read because there is neither a '-' nor '+')\n         ^\nStep 3: \"42\" (\"42\" is read in)\n           ^\nThe parsed integer is 42.\nSince 42 is in the range [-231, 231 - 1], the final result is 42.\n```\n\nSolution\n```\n#include \u003cbits/stdc++.h\u003e\nusing namespace std;\n\nclass Solution\n{\npublic:\n    int myAtoi(string s)\n    {\n        int sign = 1;\n        int base = 0;\n        int i = 0;\n        int n = s.size();\n        while (i \u003c n \u0026\u0026 s[i] == ' ')\n        {\n            i++;\n        }\n        if (s[i] == '-' || s[i] == '+')\n            sign = 1 - 2 * (s[i++] == '-');\n        while (s[i] \u003e= '0' \u0026\u0026 s[i] \u003c= '9')\n        {\n            if (base \u003e INT_MAX / 10 || base == INT_MAX / 10 \u0026\u0026 s[i] - '0' \u003e INT_MAX % 10)\n            {\n                if (sign == 1)\n                {\n                    return INT_MAX;\n                }\n                return INT_MIN;\n            }\n            else\n            {\n                base = (base * 10) + (s[i++] - '0');\n            }\n        }\n        return base * sign;\n    }\n};\nint main()\n{\n    Solution ob;\n    cout \u003c\u003c ob.myAtoi(\"-45\") \u003c\u003c endl;\n    cout \u003c\u003c ob.myAtoi(\" 56\") \u003c\u003c endl;\n    cout \u003c\u003c ob.myAtoi(\"100\") \u003c\u003c endl;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flepharamramchiary%2Fstring-to-integer-atoi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flepharamramchiary%2Fstring-to-integer-atoi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flepharamramchiary%2Fstring-to-integer-atoi/lists"}