I have the following example and need to parse only the numbers from the string:

From:

EG2594

To:

2594

or,

From: EG2594_1

To:

2594

Then I need to add a P to the extracted numbers, so it would end up as:

P2594

as my output.

What is the best, simplest way to do this in unix C?

I am thinking a character array, go through each character one by one in a loop, and comparing it to a list of 0-9.

Thanks!

Recommended Answers

All 6 Replies

You can compare each character's ASCII value. If its ASCII value is in the range of 48 to 57, inclusive, it's a digit. If not, it isn't. That would be fewer comparisons than comparing the character to each of ten digits, which would take ten comparisons. The fact that you're in Unix shouldn't make a difference I don't think.

>I am thinking a character array, go through each character
>one by one in a loop, and comparing it to a list of 0-9.
That's an option, but keep in mind that there's a digit after the underscore in your second example. It looks like you want to skip the "EG", and then take all of the digits up to the end of the string or a non-digit:

size_t i;

/* Skip the first two characters and process digits */
for ( i = 2; s[i] != '\0'; i++ ) {
  if ( !isdigit ( s[i] ) )
    break;

  process ( s[i] );
}

>If its ASCII value is in the range of 48 to 57, inclusive, it's a digit.
It's better to avoid ASCII values. C and C++ support character literals for the digits, so this test is far more portable across character sets:

if ( ch >= '0' && ch <= '9' ) {
  /* It's a digit */
}

Character types are also integer types, so you can still use a range comparison. An added bonus is that the decimal digits are required by the language standard to be contiguous.

However, there's also a standard function called isdigit that's even easier to use, shorter, and in the general case of dealing with characters, the standard is* functions take the locale into account:

if ( isdigit ( ch ) ) {
  /* It's a digit */
}

I thought you had to use the cctype library for "isdigit" and that the cctype library was a C++, not C library. You can use "isdigit" in C without having to write your own?

>I thought you had to use the cctype library for "isdigit" and that the cctype library was a C++
Any of the standard C++ headers that start with c are also available in C by removing the c prefix and adding a .h suffix. In other words, C++ has <cctype> and C has <ctype.h> with the same contents.

Well that's good to know! Guess I can stop writing my own functions now. I do feel a little silly though. Thanks for the heads up.

Member Avatar for iamthwee

>What is the best, simplest way to do this in unix C
The question that inevitably arises is, 'why are you posting this is the c++ forum?'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.