C 从用户定义的字符串中返回字符的函数

C 从用户定义的字符串中返回字符的函数,c,string,C,String,我真的用这种功能撞到了墙上。我需要创建一个从字符串返回字符的函数 用户可以输入任何数字,我需要在字符串中查找字符,其中出现的总数是用户指定的数字 有人知道如何用C语言实现吗 #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void *a, const void *b){ unsigned char ac = *(unsigned char *)a;

我真的用这种功能撞到了墙上。我需要创建一个从字符串返回字符的函数

用户可以输入任何数字,我需要在字符串中查找字符,其中出现的总数是用户指定的数字

有人知道如何用C语言实现吗
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const void *a, const void *b){
    unsigned char ac = *(unsigned char *)a;
    unsigned char bc = *(unsigned char *)b;
    return ac < bc ? -1 : ac > bc;
}

char *func(const char *str, size_t n){
    size_t len = strlen(str);
    char *dup = malloc(len + 1);
    char temp[len + 1];//[256]

    strcpy(dup, str);
    qsort(dup, len, sizeof(*dup), cmp);

    char *p = dup;
    size_t i = 0;
    while(*p){
        char aChar[] = {*p, 0};
        len = strspn(p, aChar);
        if(len == n){
            temp[i++] = *p;
        }
        p += len;
    }
    temp[i] = 0;
    strcpy(dup, temp);
    return dup;
}

int main(void){
    char *occurs = func("111abcddaaa", 1);

    for(char *p = occurs; *p; ++p){
        printf("%c\n", *p);
    }

    free(occurs);

    return 0;
}
#包括 #包括 int cmp(常数无效*a,常数无效*b){ 无符号字符ac=*(无符号字符*)a; 无符号字符bc=*(无符号字符*)b; 返回acbc; } char*func(常量char*str,大小){ 尺寸长度=strlen(str); char*dup=malloc(len+1); 字符温度[len+1];//[256] strcpy(dup,str); qsort(dup、len、sizeof(*dup)、cmp); char*p=dup; 尺寸i=0; 而(*p){ char-aChar[]={*p,0}; len=strspn(p,aChar); 如果(len==n){ 温度[i++]=*p; } p+=len; } 温度[i]=0; strcpy(dup,temp); 返回dup; } 内部主(空){ char*occurs=func(“111abcddaaa”,1); for(char*p=occurs;*p;++p){ printf(“%c\n”,*p); } 自由(发生); 返回0; }
发布迄今为止您为此编写的代码,然后人们可以帮助您使其工作。了解循环。查找
isalpha()
函数。@假设用户输入数字3,我们的测试字符串是“111abcddaaa”,它需要在字符串中找到总数为三个(或更多)的字符,然后打印出来character@ZhiZha你说数字3,给出的字符串是“111abcddaaa”,输出应该是什么?