package string; /** * Created by gouthamvidyapradhan on 21/03/2017. Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. */ public class StringToInteger { /** * Main method * @param args * @throws Exception */ public static void main(String[] args) throws Exception { System.out.println(new StringToInteger().myAtoi(" 2147483649a sdfasdf")); } public int myAtoi(String str) { boolean isPositive = true; if(str == null || str.trim().isEmpty()) return 0; str = str.trim(); if(str.charAt(0) == '+') { isPositive = true; str = str.substring(1, str.length()); } else if(str.charAt(0) == '-') { isPositive = false; str = str.substring(1, str.length()); } else if(str.charAt(0) > '9' || str.charAt(0) '9' || str.charAt(i) Integer.MAX_VALUE) return Integer.MAX_VALUE; } else { if((num * -1)