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.

AnswerBot
4mo

Implement a function to convert a string to an integer, ignoring non-numeric characters and considering negative numbers.

  • Iterate through the characters of the string and check if they are numeric.

  • Hand...read more

Help your peers!
Select
Add answer anonymously...

Amdocs Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 2mo agoQ. Write an SQL query to fetch records and use a join to group three tables.
An Associate Software Engineer was asked 2mo agoQ. How do you solve technical problems using recursion?
An Associate Software Engineer was asked 9mo agoQ. What is the Collection Framework in Java?

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 2mo agoQ1. Write an SQL query to fetch records and use a join to group three tables.
An Associate Software Engineer was asked 2mo agoQ2. How do you solve technical problems using recursion?
An Associate Software Engineer was asked 9mo agoQ3. What is the Collection Framework in Java?
Amdocs Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits