How to Filter a Number in C

The obstacle

The number has actually been blended with the text. Your objective is to recover the number from the text, can you return the number back to its initial state?

Job

Your job is to return a number from a string.

Information

You will be provided a string of numbers and letters blended, you need to return all the numbers because string in the order they take place.

The option in C

Alternative 1:

 long long filter_string( const char * s) {
long long n= 0;
char c;
while( (c= * s++)) {
if (c>>=' 0' && & & c .

long long filter_string( const char * worth) {
long long ret = 0;.

for (const char * p = worth; * p; p++) {
if (isdigit(* p)) ret = ret * 10 + * p - '0';.
}

return ret;.
}

Alternative 3:

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

long long filter_string( const char * worth) {
long long l = 0;.
char * ptr;.
char * s = calloc( strlen( worth) + 1, 1);.
char * r = s;.
while(* worth) {
if( isdigit(* worth)) * s++ = * worth;.
worth++;.
}
l = strtol( r, && ptr, 10);.
return l;.
}

Test cases to verify our option

 #

#include << criterion/criterion. h>>.
#include << stdio.h>>.
#include << stdlib.h>>.
#include << time.h>>.

void do_test( const char * worth, long anticipated);.

Test( solution_test, fixed_tests).
{
do_test(” 123″, 123);.
do_test(” a1b2c3″, 123);.
do_test(” aa1bb2cc3dd”, 123);.
}
Test( solution_test, random_tests).
{
srand( time( NULL));.

#define randomLetter() (rand() % 26 + ‘a’).
#define randomValue() (llabs((( long long) rand() << < < 32) + rand())). for (int trial = 1; trial <

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: