Program to check if input is an integer or a string - GeeksforGeeks

04 May, 2021
Write a function to check whether a given input is an integer or a string.
Definition of an integer : 
Every element should be a valid digit, i.e '0-9'.
Definition of a string : 
Any one element should be an invalid digit, i.e any symbol other than '0-9'.
Examples: 
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.
The idea is to use isdigit() function and is_numeric() function.. 
Below is the implementation of the above idea. 
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to check if a given string// is a valid integer#include <iostream>using namespace std; // Returns true if s is a number else falsebool isNumber(string s){    for (int i = 0; i < s.length(); i++)        if (isdigit(s[i]) == false)            return false;     return true;} // Driver codeint 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 program to check if a given// string is a valid integerimport 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 program to check if a given string# is a valid integer # This function Returns true if# s is a number else falsedef isNumber(s):     for i in range(len(s)):        if s[i].isdigit() != True:            return False     return True  # Driver codeif __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# program to check if a given// string is a valid integerusing 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// PHP program to check if a// given string is a valid integer // Returns true if s// is a number else falsefunction isNumber($s){    for ($i = 0; $i < strlen($s); $i++)        if (is_numeric($s[$i]) == false)            return false;     return true;} // Driver code // Saving the input// in a string$str = "6790"; // Function returns// 1 if all elements// are in range '0-9'if (isNumber($str))    echo "Integer";else    echo "String"; // This code is contributed by ajit?>
<script>    // Javascript program to check if a given    // string is a valid integer         // Returns true if s is a    // number else false    function isNumber(s)    {        for (let i = 0; i < s.length; i++)            if (s[i] < '0' || s[i] > '9')                return false;          return true;    }         // Saving the input in a string    let str = "6790";     // Function returns 1 if all elements    // are in range '0 - 9'    if (isNumber(str))      document.write("Integer");     // Function returns 0 if the    // input is not an integer    else      document.write("String");     </script>
Integer
Using special Python built-in type() function:
type() is a built-in function provided by python . type() takes object as parameter and returns its class type as its name says.
Below is the implementation of the above idea:
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 typedef isNumber(x):    if type(x) == int:         return True    else:         return False  # Driver Codeinput1 = 122input2 = '122' # Function Call # for input1if isNumber(input1):    print("Integer")else:    print("String") # for input2if isNumber(input2):    print("Integer")else:    print("String")
Integer
String
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
jit_t
ankthon
aditisinggh
divyeshrabadiya07
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters