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

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    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[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;
    }
    Last edited by Salem; 11-24-2020 at 12:55 PM. Reason: Lousy font

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    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. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    a function that counts the number of unique characters
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int uChars( char *string ) {
     int hasChar[256] = { 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++;
     return totalUC;
    }
    
    int main( int args, char * argv[] ) {
       int result = uChars("testing");
       printf( "[%i] unique characters.", result ); 
      return 0;
    }
    "without goto we would be wtf'd"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by Structure View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int uChars( char *string ) {
     int hasChar[256] = { 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++;
     return 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 subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-19-2013, 08:37 AM
  2. Replies: 2
    Last Post: 05-06-2011, 07:43 AM
  3. Unique Word count program
    By vjefcoat in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 06:51 PM
  4. Replies: 2
    Last Post: 11-10-2009, 05:35 PM
  5. Replies: 1
    Last Post: 11-05-2009, 12:30 AM

Tags for this Thread