Implement Atoi Function
You have a string 'STR' of length 'N'. Your task is to implement the atoi function, which converts the string into an integer. If the string does not contain any numbers, the function should return 0.
Input:
The first line of input contains an integer T
representing the number of test cases. Each test case consists of a single line containing the string STR
.
Output:
For each test case, return the integer derived from the given string.
Example:
Input:
Given string: '123456'
Output:
123456
Example:
Input:
Given string: '#messi10'
Output:
10
Explanation: In the example '#messi10', characters such as '#' and 'messi' are ignored, leaving only the number '10'.
Constraints:
- The string may contain any ASCII characters.
- If a character in the string is not a number, it is ignored.
- If the first character is '-', it indicates the number is negative.
- If there are no numbers in the string, return 0.
- It is guaranteed that the resulting number will be ≤ 109.
- 1 ≤ T ≤ 50
- 0 ≤ N ≤ 3*103
- Time Limit: 1 sec
Note:
You do not need to print anything; simply implement the function as specified.
Be the first one to answer
Add answer anonymously...
Top Amdocs Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Top HR questions asked in Amdocs Associate Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app