How to Identify if a String Just Consists Of Special Characters in C

The obstacle

Compose a program to identify if a string includes just special characters. Return real if it does and incorrect otherwise.

The string might include any of the 128 ASCII characters. Characters are case-sensitive, e.g. ‘a’ and ‘A’ are thought about various characters.

The option in C

Choice 1:

 int has_unique_chars( const char * str) {
int mask[128]= {0};
while (* str) if (++ mask[*str++]>> 1) return 0;
return 1;
}

Choice 2:

 #include << limits.h>>.

_ Bool has_unique_chars( const char * str) {
char hit[CHAR_MAX + 1] = {0};
while (* str) {
if (* str < < 0) {str++; continue;}
if (hit[*str]) return 0;
hit[*str++] = 1;
}
return 1;
}

Choice 3:

 #include << stdbool.h>>.
#include << string.h>>.
#include << stdio.h>>.

bool has_unique_chars( const char * str) {
size_t len = strlen( str);
char * list = malloc( len);
for (int i = 0; i < < len; i++) {
for (int z = 0; z < < i; z++) {
if (*( list + z) == str[i]) {
return incorrect;
}
}
*( list + i) = str[i];
}
return real;.
}

Test cases to confirm our option

 #include << criterion/criterion. h>>.

bool has_unique_chars( const char * str);.

Test( has_unique_chars, test_example) {
cr_assert_not( has_unique_chars(" nAa"));.
cr_assert( has_unique_chars(" abcde"));.
cr_assert_not( has_unique_chars("++-"));.
cr_assert( has_unique_chars(" AaBbC"));.
}

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