The "C Language" write function implements the library function Atoi, transforms the string into the shaping

Source: Internet
Author: User
Write a function to implement library function Atoi, convert the string into shaping # include <stdio.h> #include <string.h>int my_atoi (const char *src) {int flag=1; int Sum=0;while (*SRC) {if (*src = = ") Src++;else if (*src = = ' + ') {Src++;flag = 1;} else if (*src = = '-') {src++;flag =-1;} else if (*src >= ' 0 ' &&*src <= ' 9 ') {sum = sum * + (*SRC-' 0 '); src++;} Else{return 0;}} sum = Sum*flag;return sum;} int main () {printf ("%d\n", My_atoi ("+12345"));p rintf ("%d\n", My_atoi (" -12345"));p rintf ("%d\n", My_atoi ("+12345")); printf ("%d\n", My_atoi (" -12345"));p rintf ("%d\n", My_atoi ("123")  );p rintf ("%d\n", My_atoi ("234  5")); printf ("%d\n", My_atoi (""));p rintf ("%d\n", My_atoi ("123ab")); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The "C Language" write function implements the library function Atoi, transforms the string into the shaping

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.