Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing a string to see if a number is present and asigning that value to a variable while skipping all the non-numeric values?

given a string say " a 19 b c d 20", how do I test to see if at that particular position on the string there is a number? (not just the character '1' but the whole number '19' and '20').

char s[80];
strcpy(s,"a 19 b c d 20");

int i=0;
int num=0;
int digit=0;
for (i =0;i<strlen(s);i++){
    if ((s[i] <= '9') && (s[i] >= '0')){    //how do i test for the whole integer value not just a digit

        //if number then convert to integer
        digit = s[i]-48;
        num = num*10+digit;
    }

    if (s[i] == ' '){
        break; //is this correct here? do nothing
    }
    if (s[i] == 'a'){
       //copy into a temp char
    }
}
like image 963
silent Avatar asked Jan 30 '26 17:01

silent


2 Answers

These are C solutions:

Are you just trying to parse the numbers out of the string? Then you can just walk the string using strtol().

long num = 0;
char *endptr = NULL;
while (*s) {
  num = strtol(s, &endptr, 10);
  if (endptr == s) { // Not a number here, move on.
    s++;
    continue;
  }
  // Found a number and it is in num. Move to next location.
  s = endptr;
  // Do something with num.
}

If you have a specific location and number to check for you can still do something similar.
For example: Is '19' at position 10?

int pos = 10;
int value = 19;
if (pos >= strlen(s))
  return false;
if (value == strtol(s + pos, &endptr, 10) && endptr != s + pos)
  return true;
return false;

Are you trying to parse out the numbers without using any library routines?

Note: I haven't tested this...

int num=0;
int sign=1;
while (*s) {
  // This could be done with an if, too.
  switch (*s) {
    case '-':
      sign = -1;
    case '+':
      s++;
      if (*s < '0' || *s > '9') {
        sign = 1;
        break;
      }
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      // Parse number, start with zero.
      num = 0;
      do {
        num = (num * 10) + (*s - '0');
        s++;
      } while (*s >= '0' && *s <= '9');
      num *= sign;
      // Restore sign, just in case
      sign = 1;
      // Do something with num.
      break;
    default:
      // Not a number
      s++;
  }
}
like image 93
Harvey Avatar answered Feb 01 '26 05:02

Harvey


It seems like you want to parse the string and extract all the numbers from it; if so, here's a more "C++" way to do it:

string s = "a 19 b c d 20"; // your char array will work fine here too
istringstream buffer(s);
string token;
int num;
while (!buffer.eof())
{
    buffer >> num; // Try to read a number
    if (!buffer.fail()) {    // if it doesn't work, failbit is set
        cout << num << endl; // It's a number, do what you want here
    } else {
        buffer.clear();  // wasn't a number, clear the failbit
        buffer >> token; // pull out the non-numeric token
    }
}

This should print out the following:

19 
20

The stream extraction operator pulls out space-delimited tokens automatically, so you're saved from having to do any messy character-level operations or manual integer conversion. You'll need to #include <sstream> for the stringstream class.

like image 37
tzaman Avatar answered Feb 01 '26 05:02

tzaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!