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

if the line says "a 19 bcd 20", how can I check if there is a line in this particular position of the line? (and not just the character "1", but the integer "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
    }
}
+3
source share
6 answers

These are the C solutions:

Are you just trying to parse numbers from a string? Then you can just go through the line 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 verification number, you can still do something similar.
For example: “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;

- ?

. ...

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++;
  }
}
+4

, ; , "++" :

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 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
    }
}

:

19 
20

, - . stringstream #include <sstream>.

+1
0

if while, , .

, , ? ? , , abc123def456gh?

0

. , , - . C strtoul ( , ..).

, C isdigit , :

vector<int> parse(string const& s) {
     vector<int> vi;
     for (size_t i = 0; i < s.length();) {
         while (::isspace((unsigned char)s[ i ]) i++;
         if (::isdigit((unsigned char)s[ i ])) {
              int num = s[ i ] - '0';
              while (::isdigit((unsigned char)s[ i ])) {
                 num = num * 10 + (s[ i ] - '0');
                 ++i;
              }
              vi.push_back(num);
         }
                ....

boost::lexical_cast:

 vector<string> tokenize(string const& input) {
     vector<string> tokens;
     size_t off = 0, start = 0;
     while ((off = input.find(' ', start)) != string::npos) {
          tokens.push_back(input.substr(start, off-start));
          start = off + 1;
     }
     return tokens;
 }

 vector<int> getint(vector<string> tokens) {
      vector<int> vi;
      for (vector<string> b = tokens.begin(), e = tokens.end(); b! = e; ++b) {
         try
         {
           tokens.push_back(lexical_cast<short>(*b));
         }
         catch(bad_lexical_cast &) {}
      }
      return vi;
 }
0

, . , , , :

    int i;
    unsigned long num=0; // to hold the whole number.
    int digit;
    for (i =0;i<s[i];i++){
            // see if the ith char is a digit..if yes extract consecutive digits
            while(isdigit(s[i])) { 
                    num = num * 10 + (s[i] - '0');
                    i++;
            }
    }

It is assumed that all the digits in your string, when combined from the whole number, will not overflow the long data type.

0
source

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


All Articles