How to determine if a string has all unique characters in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
  
int count_unique_char(char* p) {
    int ascii[256] = { 0 };
    int size = strlen(p);
   
    for (int i = 0; i < size; i++) {
        ascii[p[i]] += 1;
        if (ascii[p[i]] > 1)
            return false;
    }
   
    return true;
}
   
int main() {
    char str[64] = "node.js c";
   
    printf("%s", (count_unique_char(str)) ? "yes" : "no");
       
    return 0;
}
   
   
   
   
   
/*
run:
   
yes
   
*/

 



answered May 6, 2022 by avibootz
edited May 7, 2022 by avibootz

Related questions

1 answer 105 views
1 answer 131 views
1 answer 127 views
1 answer 148 views
1 answer 120 views
...