0

I am trying to create a function that takes a string as an input and extracts the first digit as an output. Here is the relevant code. I am new to coding so tips are appreciated.

int extractNum(string id){
    int num=0;
    bool found=false;

    for(int i=0; i<id.length(); i++){
        if(isdigit(id[i])){
            num=id[i];
            found=true;
            if(found=true){
                break;
            }
        }  
    }
    return num;
};

The problem I am facing is when ever I pass a example string like "vr2498" it does take the '2' but on assigning the value to a variable or returning the value it changes to 50. Which is incorrect.

Summary: Need to extract first digit from a string.

0

3 Answers 3

2
int extractNum(string id){
    int num=0;
    bool found=false;

    for(int i=0; i<id.length(); i++){
        if(isdigit(id[i])){
            num=id[i];
            found=true;
            if(found=true){
                break;
            }
        }  
    }
    return num-'0';
}

The value you are getting is the ASCII value of 2 as '2' is still a character for the compiler and is integral value is 50 Just subtract the required character with character '0' to get the actual integral value

Sign up to request clarification or add additional context in comments.

3 Comments

You took over the weird found=true; if(found=true){break;} where break; would do the same. Do you have special reasons for that?
no, I was confused at the output tried implementing different stuff to fix it. I added those because somehow I thought the loop was affecting the outcome.
I now understand the problem.
1

Your restult num is an integer, but the "2" in your string is a character. When you assign num=id[i], you're converting the character "2" to its ASCII value, which is 50.

So, you need to convert that. The simplest way would be to just do num = id[i] - '0'. That works because in ASCII, numbers are consecutive with "0" being the first one.

The better solution here would be to use something like std::stoi which does that conversion. This would also allow converting more than one digit if that's what you want.

Also note you can just return from the loop directly, no need for break and found.

int extractNum(string id){
   for(int i=0; i<id.length(); i++){
        if(isdigit(id[i])){
            //here, you could take more than one character
            const auto digitString = id.substr(i, 1);
            return std::stoi(digitString);               
        }  
    }
    return 0;
};

5 Comments

It's better to subtract '0' as this works even in non-ASCII character sets (standard requires the digits 0-9 to follow one another immediately – if that's not the case for a character set then it is not compatible to neither C++ nor C).
@Yunnosch it seems I really can't read today
@perivesta That comment changed my perception of you completly. Before that I thought the same and was not impressed. With that comment I admire your professional ability to take feedback and see your own (if harmless) shortcomings. Cool.
Just a side note: My personal preference would rather have been char* based strtol to avoid copying the substring: return strtol(id.c_str() + i, nullptr, 10); – actually even strtoul (with adopted return type) as we don't scan for '-' either and thus negative results won't occur anyway...
Good point, I forgot strtol takes an end pointer, unlike atoi. In that case yes, that's probably faster.
0

Check the ASCII value of '2', it is 50.
You can subtract '0', to get a value 2.
This is assuming you want some hands-on.
There are functions to do this for you.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.