# Thread: A function to count the amount of unique chars in a 2D array

1. ## A function to count the amount of unique chars in a 2D array

I'm attempting to create a function that counts the number of unique characters in a 2D char array (even if 'a' occurs 4 times, it just counts it as one occurence) and the function returns true/false depending on whether the number of unique chars is higher or lower than the width or height of the grid.

I am stuck as my function is not working and I assume it's because i & j both = 0 so it is true on the first iteration. However I cannot figure out how to remedy this and create a working function, so any help would be great.

Code:
```#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#define MAXGRIDHW 9
#define FRONT -1
#define REAR -1

typedef enum bool{ false = 0, true = 1 } bool;

typedef struct node{
char grid[MAXGRIDHW][MAXGRIDHW];
int height;
int width;
int parent;
} Node;

bool uniqueChars(Node* b);
void setHeightWidth(Node* b, int h, int w);
void clearNode(Node* b);

int main (void)
{
Node shelf1;

char testArr2 = {{"z.."},
{"q.."},
{"z.."}};

clearNode(&shelf1);

memcpy(&shelf1.grid, testArr2, 7);

setHeightWidth(&shelf1, 3, 4);

assert(uniqueChars(&shelf1) == true);

return 0;
}

void clearNode(Node* b)
{
int i, j;

b->height = 0;
b->width = 0;
b->parent = FRONT;

for (i = 0; i < MAXGRIDHW; i++) {
for (j = 0; j < MAXGRIDHW; j++) {
b->grid[i][j] = '\0';
}
}
}

bool uniqueChars(Node* b)
{
int i, j, chars = 0;
bool appears;

for (i = 0; i < MAXGRIDHW; i++) {
for (j = 0; j < MAXGRIDHW; j++) {
b->grid[i][j] = toupper(b->grid[i][j]);
if (b->grid[j] == b->grid[i]) {
appears = true;
break;
}
}
if (!appears) {
chars++;
}
}

if (b->height < chars || b->width < chars) {
return false;
}
return true;
}

void setHeightWidth(Node* b, int h, int w)
{
b->height = h;
b->width = w;
}``` 2. This isn't directly related to your problem, but you can replace this:

Code:
`typedef enum bool{ false = 0, true = 1 } bool;`
with this:

Code:
`#include <stdbool.h>`
That's been a part of standard C since 1999. Welcome to (the end of) the 20th century!  3. > if (b->grid[j] == b->grid[i])
You're not comparing characters, but the (address of) the start of two arrays of characters.

Which will be of course the same if i == j 4. Also

Code:
`b->grid[i][j] = toupper(b->grid[i][j]);`
Is most likely a little bit suspect. You will be updating the contents of the grid and that might have a wider impact in a larger program. 5. ## a function that counts the number of unique characters
Code:
```#include <stdio.h>
#include <string.h>

int uChars( char *string ) {
int hasChar = { 0 }, totalUC = 1;
for ( int i=0; i<(strlen(string)-1); i++ )
hasChar[ string[i] ] = 1 ;
for ( int i=0; i<256; i++ )
if ( hasChar[i] ) totalUC++;
}

int main( int args, char * argv[] ) {
int result = uChars("testing");
printf( "[%i] unique characters.", result );
return 0;
}``` 6. Originally Posted by Structure
a function that counts the number of unique characters
You really need to stop this bad habit of posting code with no context, or in this case, taking it out of context by omitting "in a 2D char array". If you did want to provide context, then you should mention how a 2D array can be treated as 1D array, upon which code that handles a 1D array could be used. 7. Originally Posted by Structure Code:
```#include <stdio.h>
#include <string.h>

int uChars( char *string ) {
int hasChar = { 0 }, totalUC = 1;
for ( int i=0; i<(strlen(string)-1); i++ )
hasChar[ string[i] ] = 1 ;
for ( int i=0; i<256; i++ )
if ( hasChar[i] ) totalUC++;
}

int main( int args, char * argv[] ) {
int result = uChars("testing");
printf( "[%i] unique characters.", result );
return 0;
}```
Interesting. Are you running it on ARM?

Because on Intel you have a security issue as characters are signed, so:

Code:
`    hasChar[ string[i] ] = 1`
...will trash the stack. This could happen if somebody tries to process UTF-8 Unicode text. Popular pages Recent additions arrays, chars, functions, int, void 