Hỏi cách chuyển string thành dạng int hay long long mà không dùng atoi

như tiêu đề KHÔNG DÙNG atoi, mong mn chỉ mẹo

Assumption: *s is a string (or byte array)

  1. Before converting, check the contents of the string to determine whether it contains a character greater than ‘9’ or less than ‘0’. If there is a character greater than ‘9’ or less than ‘0’ (excluding ‘\0’, which indicates the end of the string), do not perform the conversion.
  2. Check from left to right to see if there is a leading ‘0’, and stop at the first character that is not a ‘0’. Remember this position as P.
  3. If the string length is less than 10, it is an int; if it is less than 19, it is a long.
  4. Iterate through the string from right to left up to position P. And start with a factor f = 1 and a sum = 0, then perform the following:
    sum += (*s - ‘0’) * f;
    f *= 10;
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?