String to INTEGER (C #),

Source: Internet
Author: User

String to INTEGER (C #),

using System;using System.Collections.Generic;namespace StrToInt_CS{    class Program    {        static void Main(string[] args)        {             string str = "";             while (!String.IsNullOrEmpty(str = Console.ReadLine()))             {                 KeyValuePair<int, bool> ret = Program.StrToInt(str);                 if (ret.Value)                 {                     Console.WriteLine("Number for {0} is {1}", str, ret.Key);                 }                 else                 {                     Console.WriteLine("The input {0} is invalid", str);                 }             }        }        public static KeyValuePair<int, bool> StrToInt(string s)        {            bool isMinus = false;            bool isValid = false;            int num = 0;            int k = 0;            // if contain leading space, then invalid            if (s[k] == ' ')            {                new KeyValuePair<int, bool>(num, isValid);            }            // if contain operator            if (s[k] == '-')            {                isMinus = true;                k++;            }            if (k < s.Length && s[k] == '+')            {                isMinus = false;                k++;            }             int flag = isMinus ? -1 : 1;            for (int i = k; i < s.Length; i++)            {                if (s[i] >= '0' && s[i] <= '9')                {                    try                    {                        // check number is not overflow                        checked                        {                            num = num * 10 + flag * (s[i] - '0');                        }                    }                    catch (Exception)                    {                        isValid = false;                        break;                    }                }                else                {                    break;                }                // if complete traverse the whole string, then is valid                 if (i + 1 == s.Length)                 {                    isValid = true;                }             }            return new KeyValuePair<int,bool>( num, isValid);        }    }}

Test cases:

Null
The input null is invalid
""
The input "" is invalid
""
The input "" is invalid

The input is invalid
123
Number for 123 is 123
+ 123
No. for + 123 is 123
-123
Number for-123 is-123
1a3333
The input 1a3333 is invalid
+ 0
Number for + 0 is 0
-0
Number for-0 is 0
2147483647
Number for 2147483647 is 2147483647
2147483648
The input 2147483648 is invalid
-2147483648
Number for-2147483648 is-2147483648
-2147483649
The input-2147483649 is invalid
+
The input + is invalid
-
The input-is invalid
++
The input ++ is invalid
__
The input _ is invalid
--
The input -- is invalid


How can I convert an integer into a string or character in C language?

Atoi: converts a string to an integer.
Itoa: converts Integers to strings.
# Include <stdlib. h>
# Include <stdio. h>
{
Int main (void)
Int number = 12345;
Char string [25];
Itoa (number, string, 10 );
Printf ("integer = % d string = % s \ n", number, string );
Return 0;
}

In C language, a problem of converting integers into character Arrays

The output is 10,
# Include <stdio. h>
Void main ()
{
Int I = 10;
Char s [10];
Sprintf (s, "% d", I );
// Itoa (I, s, 10); // same effect
Printf ("% s", s );
}
Are you sure you got it wrong?

Related Article

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.