Skip to content

Commit

Permalink
Create Number_String_check.cpp
Browse files Browse the repository at this point in the history
Checks input is string or number.
  • Loading branch information
coolster9988 authored Oct 31, 2020
1 parent 4f0d178 commit c991907
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Number_String_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 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";
}

0 comments on commit c991907

Please sign in to comment.