Write your own atoi ( ) | A simple C # program for implementation of atoi ; if whitespaces then ignore . ; sign of number ; checking for valid input ; handling overflow test case ; Driver code ; Function call
using System ; class GFG { static int myAtoi ( char [ ] str ) { int sign = 1 , Base = 0 , i = 0 ; while ( str [ i ] == ' ▁ ' ) { i ++ ; } if ( str [ i ] == ' - ' str [ i ] == ' + ' ) { sign = 1 - 2 * ( str [ i ++ ] == ' - ' ? 1 : 0 ) ; } while ( i < str . Length && str [ i ] >= '0' && str [ i ] <= '9' ) { if ( Base > int . MaxValue / 10 || ( Base == int . MaxValue / 10 && str [ i ] - '0' > 7 ) ) { if ( sign == 1 ) return int . MaxValue ; else return int . MinValue ; } Base = 10 * Base + ( str [ i ++ ] - '0' ) ; } return Base * sign ; } public static void Main ( String [ ] args ) { char [ ] str = " ▁ - 123" . ToCharArray ( ) ; int val = myAtoi ( str ) ; Console . Write ( " { 0 } ▁ " , val ) ; } }