I am using a Boolean array to check if a Char has been seen by a character occurrence check program.
My question is: How/why does/can an array boolean correlate a Char entry to true or false?
for (i = 0; i < len; i++) {
char c = text[i];
if (seen[c]==true) {
continue;
}
seen[c]=true;
Below is code snippet that includes the 'bool' in question. Note the use of bool works here, want to know why/how.
char* text = lower_case_all();
bool seen[256];
int i;
char c;
for (i = 0; i < 256; i++) {
seen[i]=false;
}
int len = strlen(text);
for (i = 0; i < len; i++) {
char c = text[i];
if (seen[c]==true) {
continue;
}
seen[c]=true;
int occs = compute_occ(c, text);
if (occs>0) {
printf("%c : %d : ",c, occs );
}
}
c
is used as an index into an array of flags indicating whether the value ofc
was ever seen in prior processing. This restricts invokingcompute_occ
to only once per any occurrence of any given value ofc
. Equatingchar
withbool
has nothing to do with that algorithm (which, incidentally, is rife with undefined behavior if you ever get values not in0x00..0x7F
inc
, andchar
is signed on your platform, as it is on most). – WhozCraigbool seen[256]; .... if (seen[c]==true) {
is WET. Consider codingif (seen[c]) {
. It may help make code clearer. – chux - Reinstate Monicachar c
,c
is used as an index into arrayseen[]
. Unfortunately,c
may be negative and thenseen[c]
is undefined behavior (UB). – chux - Reinstate Monica