2

If I have:

str1 = "str1s2"
str2 = "djfs1d2.3"

How can I get the last number in string by sscanf()?

I tried:

sscanf(str1, "%*[^0-9]%d", &n1);
sscanf(str2, "%*[^0-9]%d", &n2);

But I get only the first number:

n1 = 1
n2 = 1
4
  • reverse then reverse or loop.
    – BLUEPIXY
    May 20, 2017 at 17:43
  • @BLUEPIXY what reverse?
    – Phocs
    May 20, 2017 at 17:47
  • If str3 = "abc1d2e34"; Do you want 34 or 4 as the last "number"? May 20, 2017 at 19:05
  • @DavidC.Rankin 34
    – Phocs
    May 20, 2017 at 19:15

3 Answers 3

4

Using the %n specifier to store the number of characters processed by the scan, you can iterate through the string until scanf fails.

#include <stdio.h>

int main ( ) {
    char str2[] = "2to3";
    int span = 0;
    int index = 0;
    int lastvalue = 0;
    int value = 0;

    if ( 1 == sscanf ( &str2[index], "%d%n", &value, &span)) {
        index += span;
        lastvalue = value;
    }
    while ( 1 == sscanf ( &str2[index], "%*[^0-9]%d%n", &value, &span)) {
        index += span;
        lastvalue = value;
    }
    printf ( "last value = %d\n", lastvalue);
    return 0;
}
2
  • great! BUT why don't it work with "2to3" or "2to3-2.7"?
    – Phocs
    May 20, 2017 at 18:11
  • It prints value = 0
    – Phocs
    May 20, 2017 at 18:12
2

I personally find it to elaborative an error prone to express this solely with a scanf-pattern. I'd rather use a separate loop that iterates from the end of the string towards the beginning and positions a pointer right at the last number:

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

// extracts the last positive integral value of string s
// returns...
// on success, the int value scanned
// -1, if the string is null, empty, or not terminated by a number
int extractLastIntegral(const char* s) {
    int value = -1;
    if (s && *s) {  // don't parse null and empty strings
        const char *scanstr = s + strlen(s) - 1;
        while (scanstr > s && isdigit(*(scanstr-1))) {
            scanstr--;
        }
        sscanf(scanstr,"%d", &value);
    }
    return value;
}

int main ( ) {

    const char* teststrings[] = { "str1s2", "djfs1d2.3", "asdf3asd", "asd", "", NULL};
    ;
    for (const char** teststring=teststrings;*teststring;teststring++) {
        printf("string '%s' scans %d\n",*teststring,extractLastIntegral(*teststring));
    }
    return 0;
}
3
  • why const char *scanstr = *s ? (s + strlen(s) - 1) : s; ?
    – Phocs
    May 20, 2017 at 18:52
  • if s is an empty string, s + strlen(s) -1 would lead to scanptr = s-1, which is illegal. May 20, 2017 at 18:55
  • But you are right - it's a bit hard to read. I adapted the answer. May 20, 2017 at 18:58
1

Iterate until you fail to get a number. (Probably easier and clearer code to use a character pointer and traverse the string backwards from the end until you find a digit and then continue backwards until you find a non-digit, then scanf from there, though.)

char *cp = str1;
int nc;
while(sscanf(cp, "%*[^0-9]%d%n", &n1, &nc) == 1) cp+=nc;
printf("n1: %d\n", n1);
1
  • Why the down vote? This is the same as the subsequent/simultaneous answer that was accepted as "the answer". May 21, 2017 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.