How to Determine If a String Is a Number With C++

How to check if a string is a number?

Forget about ASCII code checks, use isdigit or isnumber (see man isnumber). The first function checks whether the character is 0–9, the second one also accepts various other number characters depending on the current locale.

There may even be better functions to do the check – the important lesson is that this is a bit more complex than it looks, because the precise definition of a “number string” depends on the particular locale and the string encoding.

How to determine if a string is a number with C++?

The most efficient way would be just to iterate over the string until you find a non-digit character. If there are any non-digit characters, you can consider the string not a number.

bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}

Or if you want to do it the C++11 way:

bool is_number(const std::string& s)
{
return !s.empty() && std::find_if(s.begin(),
s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
}

As pointed out in the comments below, this only works for positive integers. If you need to detect negative integers or fractions, you should go with a more robust library-based solution. Although, adding support for negative integers is pretty trivial.

Identify if a string is a number

int n;
bool isNumeric = int.TryParse("123", out n);

Update As of C# 7:

var isNumeric = int.TryParse("123", out int n);

or if you don't need the number you can discard the out parameter

var isNumeric = int.TryParse("123", out _);

The var s can be replaced by their respective types!

how to check if the input is a number or not in C?

Another way of doing it is by using isdigit function. Below is the code for it:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXINPUT 100
int main()
{
char input[MAXINPUT] = "";
int length,i;

scanf ("%s", input);
length = strlen (input);
for (i=0;i<length; i++)
if (!isdigit(input[i]))
{
printf ("Entered input is not a number\n");
exit(1);
}
printf ("Given input is a number\n");
}

Testing if a string is a number or character

Here is sample and working code, just change it so it suits your needs

#include <iostream>
#include <string>
#include <cctype>
#include <stdlib.h>

using namespace std;

bool isNum(char *s) {
int i = 0, flag;

while(s[i]){
//if there is a letter in a string then string is not a number
if(isalpha(s[i])){
flag = 0;
break;
}
else flag = 1;
i++;
}
if (flag == 1) return true;
else return false;
}


int main(){
char stingnum1[80], stringnum2[80];
double doublenum1, doublenum2;
cin>>stingnum1>>stringnum2;
if(isNum(stingnum1) && isNum(stringnum2)){
doublenum1 = atof(stingnum1);
doublenum2 = atof(stringnum2);
cout<<doublenum1 + doublenum2 << endl;
}
else cout<<"error";

return 0;
}

How to check if String consist of numeric or alphabet in C

like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

bool validate_data(const char rec[], int *number, char text[]){
*number = -1;//if invalid
*text = 0; //if invalid

if(strncmp(rec, "*H1", 3) != 0) return false;//invalid header

bool not_digit = false;
int i, j = 0;
for(i = 3; rec[i] && rec[i] != '#'; ++i){
text[j++] = rec[i];
if(!isdigit(rec[i]))
not_digit = true;
}
text[j] = 0;

if(rec[i] != '#'){
*text = 0;
return false;//invalid footer
}
if(not_digit == false){
if(j != 6){
*text = 0;//valid as text?
return false;//invalid number format
}
*number = atoi(text);
}
return true;
}


int main(void){
const char *test_data[] = {
"*H1000000#","*H1999999#", "*H1123456#", "*H1FINE#", "*H1MED#",
"*h123#", "**H1666666#", "*H2888888#", "*H1777#", "*H1555555"
};
int number_of_data = sizeof(test_data)/sizeof(*test_data);
char H1subbuff[10];
int H1Val;

for(int i = 0; i < number_of_data; ++i){
printf("%s :\n", test_data[i]);
if(validate_data(test_data[i], &H1Val, H1subbuff)){
if(H1Val >= 0)
printf("value is %d(%s)\n", H1Val, H1subbuff);
else
printf("text is %s\n", H1subbuff);
} else {
printf("invalid format\n");
}
puts("");
}
return 0;
}

Problem while checking if a string is a number in c

Your code only checks the first character of the string - It always returns a result based on whether or not the first character is or is not a digit.

Instead of printing the successful result within the loop, instead wait for the loop to iterate through all the characters, and only return early if a non-numeric character is found:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
# include <ctype.h>

int main(int argc, string argv[])
{
if (argc !=2)
{
printf("Usage: %s key\n", argv[0]);
return 1;
}
else
{
for (int i = 0; i <= (strlen(argv[1])); i++)
{
if (isalpha(argv[1][i]))
{
printf("Usage: ./caesar key \n");
// Return early for badly formed argument
return 1;
}
}

// Now that all chars have been validated, print the argument
printf("%s\n", argv[1]);
return 0;
}
}


Related Topics



Leave a reply



Submit