C++ program to check if a given string is valid integer - c program with parth patthar

c program with parth patthar

learn c and c++ in better way and with simple example.

Believe on your own logic

Tuesday, October 13, 2020

C++ program to check if a given string is valid integer

 

#include <iostream>

using namespace std;

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";

}


No comments:

Post a Comment