How to Count Statistics of a String in C

The difficulty

You will be offered a string and your job will be to return a list of ints detailing the count of uppercase letters, lowercase, numbers and unique characters, as follows.

 Fix(" *'&& ABCDabcde12345") =[4,5,5,3]
-- the order is: uppercase letters, lowercase, numbers and unique characters.

The option in C

Choice 1:

 void count_char_types (const char * string, anonymous counts[4]).
{
char c;.
counts[0] = counts[1] = counts[2] = counts[3] = 0;.
while(( c = * string++)).
if( c >>= 'A' && & & c='a' & & c =' 0' & & c . void count_char_types( const char * string, anonymous counts[0]).
{counts[1]= counts[2]= counts[3]= counts

= 0;.
for( const char
* p= string; * p; p
++) {if( isupper(* p)) counts

++;. else if( islower(* p)) counts[4]+ +;.
else if( isdigit(* p))
counts [0] ++;.
else counts[1] ++;.
}
}
[2] Choice 3:[3] #include < string.h >. #include < ctype.h >. void count_char_types( const char * string, anonymous counts[0]). {memset( counts, 0, 4 * sizeof (anonymous));. char c;. while(( c= * string + +)) {if (isupper( c)) + + counts[1];. else if( islower( c)) + + counts[2];
.
else if( isdigit( c)) + + counts(*<);. else + + counts[3];.}}

Test cases to confirm our option(* )#

 #include < criterion/criterion. h >. extern space do_test (const char * string, const anonymous anticipated(* ));
. #define selection( const anonymous[4]). Test( tests_suite, sample_tests). {do_test(" bgA5 < 1d-tOwUZTS8yQ", VARIETY {7, 6, 3, 2} );. do_test(" P * K4 %>[0]", VARIETY {9, 9
, 6, 9}>.);. do_test(" RYT' > s & gO -. CM9AKeH?,5317 tWGpS < * x2ukXZD", VARIETY {15, 8, 6, 9} );. do_test(" $ Cnl) Sr < 7bBW - & qLHI!mY41ODe", VARIETY {10, 7, 3, 6} );. do_test(" @mw > 0 = QD-iAx! rp9TaG?o & M % l $ 34L.
nbft", VARIETY {7, 13, 4, 10});. do_test ("", VARIETY {0, 0, 0, 0});.} [1].

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: :???: :?: :!: