Conversion between a string and the corresponding integer

Source: Internet
Author: User

The C language provides several standard library functions that convert numbers of any type (integer, long, float, and so on) to a string. The following respectively converts the string to the corresponding integer using the My_atoi () function, and converts the integer to a string using the Itoa () function.

atoi Converting a string to an integer number
Itoa converting an integer to a string

1, implement a function int my_atoi (char s[]), you can convert a string to the corresponding integer.

For example: Enter the string "1234" and return the number 1234.

Enter the string "+1234" and return the number 1234.

Enter the string "1234" and return the number-1234.

The code is as follows:

#include <stdio.h> #include <assert.h> #include <ctype.h> #include <stdlib.h>int my_atoi (char * p) {int flag=1,rav=0;assert (p), while (Isspace (*p))//Determine if the pointer is a white-space character {*p++;} if (*p== '-') {Flag=-1;//flag save symbol Plus/minus}if (*p== '-' | | *p== ' + ')//If the sign bit is moved after {p++;} while (*P)//assigns each bit of the number to Rav, which is equivalent to while (*p!= ') {if (*p>= ' 0 ') && (*p<= ' 9 ')) {rav=rav*10+ (*p-' 0 ');        /*p-' 0 ' is the number}p++ each time it is taken out;}        Return flag*rav;//returns the product of the symbol and number}int main () {char str[10]; Gets (str);//Enter a string printf ("%d\n", My_atoi (str)); system ("pause"); return 0;}

2 . Implement a function itoa (int n,char s[]), convert the number of integer n to the corresponding string, and save to S.

The code is as follows:

#include <stdio.h> #include <stdlib.h>void reverse (char str[],int len)//reverse the entire character array {int  left=0;int right=len-1;while (left<right) {Char temp=str[left];str[left]=str[right];str[right ]=temp;left++;right--;}} Void itoa (int num, char str[]) {    int i=0,j,flag;     flag=num;//flag Save Symbol     if (num<0)//Convert to positive if the number entered is negative       num=-num;    while (num)     {str[i++]=num%10+ ' 0 ';//number in reverse order in character array     num/=10;}     if (flag<0) {str[i++]= '-';///character array stored in symbol}    str[i]= ' + ';     reverse (str,i);} Int main (void) {&NBSP;CHAR&NBSP;STR[20];INT&NBSP;NUM;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &num); Itoa (NUM,STR);p rintf ("%s\n", str);     system ("Pause");     return 0;}

This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1709806

Conversion between a string and the corresponding integer

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.