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[10];
char testArr2[3][4] = {{"z.."},
{"q.."},
{"z.."}};
clearNode(&shelf1[1]);
memcpy(&shelf1[2].grid, testArr2, 7);
setHeightWidth(&shelf1[2], 3, 4);
assert(uniqueChars(&shelf1[2]) == 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;
}