Integer to string, string to integer

Source: Internet
Author: User

Question: enter a string that represents an integer, convert it to an integer, and output it. For example, if the input string is "345", an integer of 345 is output.

The question is simple, but it involves many problems, such as illegal input, positive and negative signs, whether it is a null string, and not a large number type. You can use long, 8-byte integer, and so on. Mainly: int A = A * 10 + (STR [I]-'0 ')

You can also directly use the C library function, atoi function. If the conversion fails, 0 is returned. The example is as follows:

String A = "-1232 pighehe ";

Int num = atoi ();

 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 enum flag{ 5     valid=0, 6     invalid=1 7 }; 8 int judge=valid; 9 10 11 long long paraInt(string str)12 {13     judge=valid;14     bool Fuflag=false;15     long long num=0;16     int i=0;17     if(str.size()==0) 18     {19         judge=invalid;20         return invalid;21     }22     if (str[0]==‘-‘)23     {24         i=1;25         Fuflag=true;26         if(str.size()==1) 27         {28             judge=invalid;29             return invalid;30         }31     }32     33     for (i;i<str.size();++i)34     {35         int temp=str[i]-‘0‘;36         if(temp>=0&&temp<=9)37             num=num*10+temp;38         else{39             judge=invalid;40             return invalid;41         }42     }43     if(Fuflag)44         a=-a;45     return num;46 }47 int main()48 {49     string str;50     while(cin>>str)51     {52         long long num=paraInt(str);53         if(!judge)54             cout<<num<<endl;55         else56         {57             cout<<"invalid input"<<endl;58         }59         str.clear();60     }61     return 0;62 }

Question: enter an integer to convert it into a string and output it. For example, if you enter an integer of 345, an integer of 345 is output ".

Similar functions include ITOA. The example is as follows:

Int num = 12345;
Char STR [10];
ITOA (Num, STR, 10); // convert num into a string and save it to Str.

 

If you do not use the ITOA function, convert each integer to a character and save it in reverse order. For example:

//整形转成字符串函数实现  //题目不难,重点考察面试者对问题考虑的全面程度  #include <iostream>  using namespace std;  void itoa_mf(int num,char str[])  {      int sign = num;      int i = 0;      int j = 0;      char temp[100];      //如果是负数就去掉符号,将-1234转成1234      if(sign < 0)      {          num = -num;      }      //转成字符串,1234转成"4321"      do      {          temp[i] = num % 10 + ‘0‘;          num /= 10;          i++;      }while(num > 0);      //如果是负数的话,加个符号在末尾,如:"4321-"      if(sign < 0)      {          temp[i++] = ‘-‘;      }      temp[i] = ‘\0‘;      i--;      //将temp数组中逆序输入到str数组中      //将"4321-" ====> "-1234"      while(i >= 0)      {          str[j] = temp[i];          j++;          i--;      }      //字符串结束标识      str[j] = ‘\0‘;  }      //测试用例  void main()  {      int a = 0;      char s[100];      itoa_mf(a,s);      cout << s << endl;    }  

 

Integer to string, string to 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.