新手求教 检测一个字符串中共包含有多少种字符
检测一个字符串中共包含有多少种字符
程序代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> int countType(const char *s) /* FIXME 只支持ASCII */ { char *buffer = calloc(sizeof (char), strlen(s)+1); int bufferNo = 0; memset(buffer, '\0', sizeof (char) * (strlen(s)+1)); for (; *s; ++s) { if (NULL == strchr(buffer, *s)) { buffer[bufferNo++] = *s; } } free(buffer); return bufferNo; } int main(int argc, char *argv[]) { char s[] = "abcdefaa,./12456"; printf("%s %d\n", s, countType(s)); return 0; }