atoi 实现 + string转int + int转string

//j#include <stdio.h>
//#include <stdlib.h>
#include <string>
//#include <ctype.h>//isspace 头文件
#include <iostream>
using namespace std;

long my_atoi(const char *a)
{
    
     while(isspace((int)(unsigned char)*a))
     {
         ++a;                         
     }
     int c;
     c = (int)(unsigned char)*a++;
     int sign = c;
     int total=0;
    
     while(isdigit(c))
     {
          total = total*10 + (c-'0');
          c = (int)(unsigned char)*a++;            
     }
     if(sign=='-')
                  return -total;
     else
         return total;   
}

 

int main()
{
    char *a="123345";  
    int b;
    b= 0;
   
    int num=2349535;
    char n[10];
    itoa(num,n,10);  //int to char*
    printf("%s\n",n);
   
    char m[10];
    sprintf(m,"%d",num);//int to char*
    printf("%s\n",m);
   
    string s=m;
   // cout<<s<<endl;
   printf("%s\n",s.c_str());//string 不是C的内置数据类型,,,,,所以一般用cout.
   
   
    int k=0;
    k=atoi(a);
    printf("%d\n",k);
   
    string aa="       3456    ";
    const char *p=aa.c_str();
    b=my_atoi(p);
    printf("%d\n",b);
 
    char buff[10];
    strcpy(buff,aa.c_str());
    sscanf(buff,"%d",&b);   
    printf("%d\n",b);
       
    system("pause");
    return 0;
}

你可能感兴趣的:(c,String,System)