Advertisement
jewalky

Untitled

Aug 7th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. function int StrToInt(str s)
  2. {
  3.     int integer = 0;
  4.     bool negative = GetChar(s, 0)=='-';
  5.     int j;
  6.     int i = 0;
  7.     if (GetChar(s, 0) == '-' || GetChar(s, 0) == '+')
  8.         i++;
  9.    
  10.     for(; i < StrLen(s); i++)
  11.     {
  12.         int c = GetChar(s, i);
  13.         if (c < '0' || c > '9') return 0; // invalid int
  14.         c -= '0';
  15.         int multiplier = StrLen(s)-i;
  16.         for (j = 1; j < multiplier; j++)
  17.             c *= 10;
  18.         integer += c;
  19.     }
  20.    
  21.     if (negative) integer = -integer;
  22.     return integer;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement