Converting strings to Integers-a simple challenge for hero Pang Guo

Source: Internet
Author: User

Just idle, I went around csdn and saw this programming challenge: Converting strings to integers. The requirements are as follows:

Question details

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

Complete the strtoint function to convert a string to an integer.

Reminder:

Before submitting the code, please review your program. For example, if the given string is shown in the picture on the left, are you sure you want to take it into consideration?

Of course, the correct output corresponding to each of them is shown in the picture on the right (assuming that you are in a 32-bit System and the compiling environment is vs2008 or above)

I haven't done this kind of question for a long time. The question should be completed in Java, C, C ++, C. of course, I chose C #. I tried it. below is the code I submitted and I don't expect to win the prize, because hundreds of people have submitted the correct solution (that is, the challenge is successful)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace test{    class Program    {        static void Main(string[] args)        {            string[] ar = { "", "1", "+1", "-1", "123", "-123", "010", "+00131204", "-01324000", "2147483647", "-2147483648", "-2147483649", "2147483648", "-2147483648", "abc", "-abc", "1a", "23a8f", "-3924x8fc", "   321", "   -321", "123  456", "123   ", "   -321", "   +4488", "  +  4488", "  +  413", "  ++c", " ++1", " --2", "    -2" };            foreach (var s in ar)            {                Console.WriteLine("{0}->{1}", s, StrToInt(s));            }            Console.Read();        }        static int StrToInt(string str)        {            if (str == null)            {                throw new ArgumentException("str can't be null!");            }            if (str == "")            {                return 0;            }            str=str.Trim();            bool singed = false;            int index = 0;            if (str[0] == '-' || str[0] == '+')            {                singed = true;                index = 1;            }            int length = str.Length;            int n = 0;            while (index < length)            {                if (char.IsNumber(str[index]))                {                    n = n * 10 + (str[index++] - '0');                    if (n < 0)                    {                        if (singed)                        {                            if (str[0] == '+')                            {                                return 2147483647;                            }                            else                            {                                return -2147483648;                            }                        }                        else                        {                            return 2147483647;                        }                                            }                }                else                {                    break;                }            }            if (singed && str[0] == '-')            {                n *= -1;            }            return n;        }    }}

I haven't written C # for a long time. I just want to entertain myself. .

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.