C언어 문자열에 중복 문자 있는지 확인

(C언어 문자열에서 중복 문자가 있는지 확인에서 넘어옴)

1 개요[ | ]

Check the duplicate in string
문자열에서 중복 문자가 있는지 확인

2 예시 1[ | ]

#include <stdio.h>
#include <string.h>

int allUnique(char *str) {
    int i, j;
    char *p = str;
    int l = strlen(str);

    for(i = 0; i < l - 1; i++) {
        for(j = i + 1; j < l; j++) {
            if(p[i] == p[j]) return 0; 
        }
    }
    return 1; 
}

int main() {
    printf("%d\n", allUnique("abcd")); // 1
    printf("%d\n", allUnique("abcc")); // 0
}

3 예시 2[ | ]

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool allUnique(const char *str) {
    if (str == NULL) {
        return false;
    }
    bool isCharPresent[256] = {0};
    int length = strlen(str);
    for (int i = 0; i < length; i++) {
        unsigned char ch = str[i];
        if (isCharPresent[ch]) {
            return false;
        }
        isCharPresent[ch] = true;
    }
    return true;
}

int main() {
    printf("%d\n", allUnique("abcd")); // 1
    printf("%d\n", allUnique("abcc")); // 0
}

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}