Q:

C++ program to check given string is numeric or not

0

cC++ program to check given string is numeric or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

#include <iostream>
using namespace std;

int isNumericString(unsigned char *num)
{
	int i=0;
    while (*(num+i)) {
        if (*(num+i) >= '0' && *(num+i) <= '9')
            i++;
        else
            return 0;
    }
    return 1;
}

int main()
{
	int ret = 0;
	unsigned char str1[] = "123";
	unsigned char str2[] = "ABC";
	
	ret = isNumericString(str1);
	if(ret)
		cout<<"It is numeric string"<<endl;
	else
		cout<<"It is not numeric string"<<endl;
	
	ret = isNumericString(str2);
	if(ret)
		cout<<"It is numeric string"<<endl;
	else
		cout<<"It is not numeric string"<<endl;	
	
	return 0;
}

Output

It is numeric string
It is not numeric string

See the program; here str1 contains numeric string while str2 does not contain numeric string. Program is validating the string as numeric or not.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to set IP address, subnet mask, networ... >>
<< C++ program to check given date is in valid format...