String to integer, string to integer

Source: Internet
Author: User

String to integer, string to integer

Question: convert a string to an integer. For example, input "123" and output 123.


Solution:

A 0-9 integer number is obtained using the Ascii value corresponding to each 0-9 digit minus 48. Multiply the integer number represented by the first I-1 digit by the number 10 plus the number indicated by the I-digit, you can get the converted integer. Note that the string can contain 1st characters, which can be + or -. When the string is 1st characters is-, the integer number represented by the first I-1 is multiplied by 10 minus the number represented by the I bit, you can get the converted integer. In this case, the preceding number is 0. For example, input "-0123" and output-123. Another thing to note is overflow: the integer converted from a string cannot exceed the range where the integer can represent numbers.


Int strToInt (char str []) {int max = (1 <30)-1 + (1 <30); // The maximum number that a 32-bit system integer can represent, the reason for not using (1 <31)-1 is that integer overflow occurs when 1 <31, although only one warning int min = (-1 <31) is reported ); // The minimum integer value that can be expressed by a 32-bit system. int number = 0; // number indicates the integer that the string is converted to. When the string cannot be converted, will prompt and return 0if (str = NULL) // when the string is NULL {printf ("this string is empty and cannot be converted to an integer"); return 0 ;} int n = 0; while (str [n]! = '\ 0') // indicates whether illegal characters exist in the traversal string {if (n = 0) // The 1st characters in the string can be 0-9 digits or + or-{if (str [n]> = 48 & str [n] <= 57) | str [n] = '+' | str [n] = '-') {} else {printf ("this string cannot be converted to an integer "); return 0 ;}} else // string except 1st characters, all other characters must be 0-9 {if (str [n]> 57 | str [n] <48) {printf ("this string cannot be converted to an integer "); return 0 ;}} n ++;} if (str [0] = '\ 0') // when the string is empty, {printf ("this string is empty, cannot be converted to integer "); return 0;} else if (str [0] = '+') // when the first character of the string is + {int I = 1; while (str [I]! = '\ 0') {// determines whether the converted integer will overflow if (number> max/10) {printf ("converting this string to an integer will overflow, cannot be converted to integer "); return 0;} else if (number = max/10) {if (str [I]-48> max % 10) {printf ("this string converted to an integer will overflow and cannot be converted to an integer"); return 0 ;}number = 10 * number + (str [I]-48 ); I ++;} return number;} else if (str [0] = '-') {int I = 1; while (str [I]! = '\ 0') {// determines whether the converted integer will overflow if (number <min/10) {printf ("this string converted to an integer will overflow, cannot be converted to integer "); return 0;} else if (number = min/10) {if (-(str [I]-48) <min % 10) {printf ("this string converted to an integer will overflow and cannot be converted to an integer"); return 0 ;}number = 10 * number-(str [I]-48 ); I ++;} return number;} else {int I = 0; while (str [I]! = '\ 0') {// determines whether the converted integer will overflow if (number> max/10) {printf ("converting this string to an integer will overflow, cannot be converted to integer "); return 0;} else if (number = max/10) {if (str [I]-48> max % 10) {printf ("this string converted to an integer will overflow and cannot be converted to an integer"); return 0 ;}number = 10 * number + (str [I]-48 ); I ++;} return number ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.