C 从用户定义的字符串中返回字符的函数
我真的用这种功能撞到了墙上。我需要创建一个从字符串返回字符的函数 用户可以输入任何数字,我需要在字符串中查找字符,其中出现的总数是用户指定的数字 有人知道如何用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;
#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;
返回ac发布迄今为止您为此编写的代码,然后人们可以帮助您使其工作。了解循环。查找
isalpha()
函数。@假设用户输入数字3,我们的测试字符串是“111abcddaaa”,它需要在字符串中找到总数为三个(或更多)的字符,然后打印出来character@ZhiZha你说数字3,给出的字符串是“111abcddaaa”,输出应该是什么?