ronith
8/13/2018 - 9:47 AM

Program to check if input is an integer or a string

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.

#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    getline(cin, s);
    int c=0;
    for (int i=0;i<s.length();i++)
        if ((s[i]- '0' > 9) || (s[i]- '0' < 0)){
            cout<< "Not an integer\n";
            exit(0);
        }
    cout<< "Integer :)";

}