📌  相关文章
📜  检查输入是整数还是字符串的程序

📅  最后修改于: 2022-05-13 01:57:07.627000             🧑  作者: Mango

检查输入是整数还是字符串的程序

编写一个函数来检查给定的输入是整数还是字符串。

整数的定义:

Every element should be a valid digit, i.e '0-9'.

字符串的定义:

Any one element should be an invalid digit, i.e any symbol other than '0-9'.

例子:

Input : 127
Output : Integer
Explanation : All digits are in the range '0-9'.

Input : 199.7
Output : String
Explanation : A dot is present. 

Input : 122B
Output : String
Explanation : A alphabet is present.

这个想法是使用 isdigit()函数和 is_numeric()函数..

下面是上述思想的实现。

C++
// CPP program to check if a given string
// is a valid integer
#include 
using namespace std;
 
// Returns true if s is a number else false
bool isNumber(string s)
{
    for (int i = 0; i < s.length(); i++)
        if (isdigit(s[i]) == false)
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    // Saving the input in a string
    string str = "6790";
 
    // Function returns 1 if all elements
    // are in range '0-9'
    if (isNumber(str))
        cout << "Integer";
 
    // Function returns 0 if the input is
    // not an integer
    else
        cout << "String";
}


Java
// Java program to check if a given
// string is a valid integer
import java.io.*;
 
public class GFG {
 
    // Returns true if s is
    // a number else false
    static boolean isNumber(String s)
    {
        for (int i = 0; i < s.length(); i++)
            if (Character.isDigit(s.charAt(i)) == false)
                return false;
 
        return true;
    }
 
    // Driver code
    static public void main(String[] args)
    {
        // Saving the input in a string
        String str = "6790";
 
        // Function returns 1 if all elements
        // are in range '0 - 9'
        if (isNumber(str))
            System.out.println("Integer");
 
        // Function returns 0 if the
        // input is not an integer
        else
            System.out.println("String");
    }
}
 
// This code is contributed by vt_m.


Python 3
# Python 3 program to check if a given string
# is a valid integer
 
# This function Returns true if
# s is a number else false
def isNumber(s):
 
    for i in range(len(s)):
        if s[i].isdigit() != True:
            return False
 
    return True
 
 
# Driver code
if __name__ == "__main__":
 
    # Store the input in a str variable
    str = "6790"
 
    # Function Call
    if isNumber(str):
        print("Integer")
 
    else:
        print("String")
 
# This code is contributed by ANKITRAI1


C#
// C# program to check if a given
// string is a valid integer
using System;
 
public class GFG {
 
    // Returns true if s is a
    // number else false
    static bool isNumber(string s)
    {
        for (int i = 0; i < s.Length; i++)
            if (char.IsDigit(s[i]) == false)
                return false;
 
        return true;
    }
 
    // Driver code
    static public void Main(String[] args)
    {
 
        // Saving the input in a string
        string str = "6790";
 
        // Function returns 1 if all elements
        // are in range '0 - 9'
        if (isNumber(str))
            Console.WriteLine("Integer");
 
        // Function returns 0 if the
        // input is not an integer
        else
            Console.WriteLine("String");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


Python3
# Python program to find
# whether the user input
# is int or string type
 
# Function to determine whether
# the user input is string or
# integer type
def isNumber(x):
    if type(x) == int:
         return True
    else:
         return False
 
 
# Driver Code
input1 = 122
input2 = '122'
 
# Function Call
 
# for input1
if isNumber(input1):
    print("Integer")
else:
    print("String")
 
# for input2
if isNumber(input2):
    print("Integer")
else:
    print("String")


输出
Integer

使用特殊的Python内置 type()函数:

type() 是Python提供的内置函数。 type() 将对象作为参数,并按照其名称返回其类类型。

下面是上述思想的实现:

Python3

# Python program to find
# whether the user input
# is int or string type
 
# Function to determine whether
# the user input is string or
# integer type
def isNumber(x):
    if type(x) == int:
         return True
    else:
         return False
 
 
# Driver Code
input1 = 122
input2 = '122'
 
# Function Call
 
# for input1
if isNumber(input1):
    print("Integer")
else:
    print("String")
 
# for input2
if isNumber(input2):
    print("Integer")
else:
    print("String")
输出
Integer
String