# String to integer

Title: input a string representing integer, convert the string to integer and output. For example, input string "345" will output integer 345.

Idea: for example, "345", the first scan to 3, the second scan to 4, at this time should let 3 * 10 + 4, get 34. Continue scanning to 5, you should let 34 * 10 + 5 to get 345. So it can be concluded that for each character scanned, we multiply the previous number by 10 and add the current character.

Boundary and special conditions:
1. Integers may not only have numbers but also symbols, starting with '+' or '-'. If the first string is' + ', no processing will be performed. If it is' -', the final value will be negative.

2. Illegal input. Since the input is a pointer, we need to determine whether the pointer is null.

3. Illegal characters. The input string may contain non numeric characters. When an illegal character is encountered, the conversion stops.

4. Overflow. Because the input number is in the form of string, it is possible that the large number will lead to the overflow of the converted integer.

``````#include<iostream>
#include <limits>
using namespace std;

int StrToInt(const char* str)
{

long long num = 0;

if(str != NULL)
{

//Determine whether the first string is a symbol
bool minus = false;  //Is it a minus sign? Yes, the result should be negative
if (*str == '+')
{
}
else if (*str =='-')
{
str ++;
minus = true;
}

//Convert the remaining characters
while(*str != '\0') //Judgment is not a space
{
if(*str >= '0' && *str <= '9') //Between 0 and 9
{
num = num * 10 + (*str - '0');

//Judgement overflow
if (num > numeric_limits<int> :: max())
{
cout << "overflow";
num = 0;
break;
}

str ++;
}

//If the character is not a number, it is not a valid character
else
{
cout << "Illegal character";
num = 0;
break;
}
}

if (*str == '\0')
{
if (minus)
num = 0 - num;
}
}
else
{
cout << "Null pointer exception";
}

return num;
}

// test case
void Test1()
{
int a = StrToInt("12345");
cout << "expected: 12345";
cout << endl;
cout << "result  : " << a << endl;
}

void Test2()
{
int a = StrToInt("+12345");
cout << "expected: 12345";
cout << endl;
cout << "result  : " << a << endl;
}

void Test3()
{
int a = StrToInt("-12345");
cout << "expected: -12345";
cout << endl;
cout << "result  : " << a << endl;
}

void Test4()
{
int a = StrToInt(NULL);
//  Cout < < expected: null pointer exception;
cout << endl;
cout << "result  : " << a << endl;
}

void Test5()
{
int a = StrToInt("12a45");
//  Cout < < expected: illegal input;
cout << endl;
cout << "result  : " << a << endl;
}

void Test6()
{
int a = StrToInt("1234511111111111111111111");
//Cout < < expected: overflow;
cout << endl;
cout << "result  : " << a << endl;
}

int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();

}
``````

Why does a numeric character subtract from '0' to get the numeric value of that numeric character?

This is because the C standard stipulates that the char value of the ten digits 0-9 must be continuously increasing, that is to say, '1' - '0' is equal to 1, which is the guarantee of the standard.
By the way, the C standard does not stipulate that the char value of letters must be continuous, that is to say, the standard does not guarantee that 'B'-'A' == 1 must be tenable.

Posted on Tue, 31 Mar 2020 03:35:10 -0700 by AKA Panama Jack