Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save iamutkarshtiwari/1ebebf9d40eaf8c02284cfbb013b30a2 to your computer and use it in GitHub Desktop.

Select an option

Save iamutkarshtiwari/1ebebf9d40eaf8c02284cfbb013b30a2 to your computer and use it in GitHub Desktop.
/*You are required to complete this method */
class GfG
{
int atoi(String str)
{
int number = 0;
if (str.length() == 0)
return -1;
for (int i = 0; i< str.length() ; i++ ) {
int c = str.charAt(i);
if ( !(c >= 48 && c<= 57)) {
return -1;
} else {
number = (c-48)*(Math.pow(10, (str.length() - 1 - i ))) + number;
}
}
return number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment