forked from xieqilu/Qilu-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.CheckIfStrIsInt.cs
More file actions
39 lines (34 loc) · 780 Bytes
/
Copy path24.CheckIfStrIsInt.cs
File metadata and controls
39 lines (34 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Check if a string is an int
//input: "string" output: false
//input: "123123" output: true
using System;
namespace CheckIfStringIsInt
{
public class Checker
{
public static bool CheckInt(string str)
{
if (str == "") //handle edge case
return false;
int length = str.Length;
for (int i = 0; i < length; i++) {
int diff = (int)str [i] - 48; //the ASCII of 0-9 is 48-57
if (diff > 9 || diff < 0)
return false;
}
return true;
}
}
class MainClass
{
public static void Main (string[] args)
{
string test1 = "123131";
string test2 = "asdasdas";
string test3 = "";
Console.WriteLine (Checker.CheckInt (test1));
Console.WriteLine (Checker.CheckInt (test2));
Console.WriteLine (Checker.CheckInt (test3));
}
}
}