Trying to determine if a string contains a period

I am trying to determine if a string contains a specific character, namely a period. I use this to determine if a number is a real number or an integer. The first condition is that the string must contain a number from 0 to 9 to be considered a number, then if it has a period (decimal), it will be considered a real number. If there is no period, then this is an integer. I can’t understand what I'm doing wrong, thanks in advance!

void typenumber(char *str)
{
  int i=0;

  if(str[i]>='0' && str[i]<='9')
  {
    if(str[i]=='.')
    {    
       printf("String %s is a real number.\n", str);
    }
    else
    {
        printf("String %s is an integer.\n", str);
    }
  }
  return;
}
+4
source share
5 answers

I am trying to determine if a string contains a certain character, in particular a period.

This can be checked with strchr(), for example:

if (strchr(str, '.') != NULL) {
   printf("String %s is a real number.\n", str);
}
else {
    printf("String %s is an integer.\n", str);
}

, '.' , . , , :

#define INT_NUM  1
#define REAL_NUM 2

int typenumber(char *str) {
        int num_type = INT_NUM;

        if ((str == NULL) || (*str == '\0')) {
                printf ("Invalid input\n");
                return 0;
        }

        while (*str != '\0') {
                if (!isdigit(*str)) {
                        if ((*str == '.') && (num_type == INT_NUM)) {
                                num_type = REAL_NUM;
                        } else {
                                return -1;
                        }
                }
                str++;
        }

        return num_type;
}
+3

, strtol(), .

, isdigit() .

+2

!

i 0, str.

0

,

int typeNumber(char *str)
{
    if(isNumericDecimal(str))
    {
        if (strchr(str, '.') != NULL) return 1; //Prime, string contain dot
        else return 0; //Integer, string does not contains dot
    }
    else return - 1; //Letter or simbol
}

int isNumericDecimal (const char * s)
{
    if (s == NULL || *s == '\0' || isspace(*s))
      return 0;
    char * p;
    strtod (s, &p);
    return *p == '\0';
}

void typenumber(char *str)
{
    if(typeNumber(str) == 1)
    {    
       printf("String %s is a real number.\n", str);
    }
    if(typeNumber(str) == -1)
    {
        printf("String %s is not a numeric value", str);
    }
    if(typeNumber(str) == 0)
    {
        printf("String %s is an integer.\n", str);
    }
  return;
}
0

, str[i]>='0' && str[i]<='9' , str[i] '.'

if(str[i]>='0' && str[i]<='9') {
  if(str[i]=='.')

, , .

, -, , " , ". : "123e4" '.', "".

"", "", . 2 .

C C strto...() . .

#include <stdlib.h>

bool is_integer(const char *s) {
  char *endptr;
  long long ll = strtoll(s, &endptr, 0);
  if (s == endptr) return false;  // no conversion;
  if (*endptr) return false;  // extra junk, maybe a `.`?
  // maybe add tests for overflow
  return true;
}

bool is_real(const char *s) {
  char *endptr;
  double d = strtod(s, &endptr);
  if (s == endptr) return false;  // no conversion;
  if (*endptr) return false;  // extra junk, maybe an `x`?
  // maybe add tests for overflow
  return true;
}

void classify(const char *s) {
  bool i = is_integer(s);
  bool r = is_real(s);
  if (i) {
    if (r) puts("Both");
    else puts("Integer");
  } else {
    if (r) puts("Real");
    else puts("Neither");
  }
}

When the string "Both" and the integer conversion does not overflow, the "integer" can be approved compared to "Real". However, if "Both" is encountered with an input like "1234567890123456789012345678901234567890", it might be better to use it as a "real" one, since usually an integer type does not exist.

0
source

Source: https://habr.com/ru/post/1695373/


All Articles