计算C中数字串中最频繁的数字

计算C中数字串中最频繁的数字,c,pointers,C,Pointers,我试图计算字符串中最频繁的数字,我需要使用指针,但我不确定如何使用指针 int most(char* string){ int counter = 0; int* array =(int*) malloc(sizeof(int)*10); char* original = string; while(*original){ counter++; string++; //Not sure what to put in

我试图计算字符串中最频繁的数字,我需要使用指针,但我不确定如何使用指针

int most(char* string){
    int counter = 0;
    int* array =(int*) malloc(sizeof(int)*10);
    char* original = string;
    while(*original){
        counter++;
        string++;
        //Not sure what to put in this loop
    }
}
例如,我想调用代码

char nums[] = "132433423";
printf("%d \n",most(nums));
// 3

您的功能规范不完整:

  • 字符串是否可以包含非数字字符
  • 如果根本没有数字,应该返回什么
  • 如果有多个数字具有相同的最大出现次数,则应返回哪个值
  • 函数应该返回数字还是其数值?您的
    main()
    函数使用后者,但从问题文本中看不清楚
most
函数接收指向字符串的指针。可以编写一个循环,每次处理一个字符,并为下一次迭代递增指针,直到到达字符串末尾。如果字符串不包含数字,您还必须决定返回什么

下面是一个简单的例子:

int most(const char *s) {
    int count[10] = { 0 };
    int res, i, max;

    while (*s) {
        if (*s >= '0' && *s <= '9')
            count[*s - '0']++;
        s++;
    }
    res = -1;  /* return -1 if no digits */
    max = 0;
    for (i = 0; i < 10; i++) {
        if (count[i] > max)
            res = i;
    }
    return res;
}

int-most(const-char*s){/*带有旧式三角图的版本*/
整数计数??(10??)={0};
int res,i,max;
而(*s){
如果(*s>='0'&&*s最大值)
res=i;
}
返回res;
}

我不知道你说的“使用指针”是什么意思,但这里有一个版本,除了遍历输入字符串外,它不使用指针:

char most_frequent_character(char *s)
  {
  int freq[10];
  int max_freq;
  int max_idx;
  int idx;

  while(*s)
    freq[*s++ - '0']++;  /* compute character freqs */

  max_idx = 0;
  max_freq = freq[0];

  for(idx = 1 ; idx < 10 ; ++idx)
    if(freq[idx] > max_freq)
      {
      max_freq = freq[idx];
      max_idx = i;
      }

  return '0' + max_idx;
  }
B.在“计算字符频率”循环中,使用指针引用而不是索引:

while(*s)
  {
  *(freq + (*s - '0')) = *(freq + (*s - '0')) + 1;
  s++;
  }
  for(idx = 1 ; idx < 10 ; ++idx)
    if( *(freq + idx) > max_freq)
      {
      max_freq = *(freq + idx);
      max_idx = i;
      }
C.使用指针ref设置
max\u freq
的初始值:

max_freq = *freq;
D.在
for
循环中,使用指针数学而不是索引:

while(*s)
  {
  *(freq + (*s - '0')) = *(freq + (*s - '0')) + 1;
  s++;
  }
  for(idx = 1 ; idx < 10 ; ++idx)
    if( *(freq + idx) > max_freq)
      {
      max_freq = *(freq + idx);
      max_idx = i;
      }
现在,坐下来,理解为什么事情是这样做的。例如,为什么我在计算角色频率时不做以下操作

while(*s++)
  *(freq + (*s - '0')) = *(freq + (*s - '0')) + 1;

以上每一项都将节省几行代码——为什么不应该使用它们呢?(显而易见的答案是“因为它们不会按预期工作”——但为什么?)

祝你好运。

#包括
#include<string.h>    
int most (char* nums) {
    int i, max_index = 0;
    int digit_dictionary[10]={0,0,0,0,0,0,0,0,0,0};

    for (i=0; i< strlen(nums); i++) {
        digit_dictionary[nums[i]-'0'] ++;
    }

    for (i=1; i<10; i++) {
        if (digit_dictionary[i]> digit_dictionary[max_index])
            max_index = i;
    }
    return max_index;
}
整数most(字符*nums){ int i,最大指数=0; 整数数字字典[10]={0,0,0,0,0,0,0,0,0,0}; 对于(i=0;i
我将尽可能地详细说明:

创建一个字典,其中每个索引对应一个可能出现的数字(0-9)。然后,遍历字符串(基本上是一个字符数组),并将每个数字存储到字典中相应的索引中。
注意
[nums[i]-“0']
计算到字典的索引中,因为每个字符都有一个整数值(查找ASCII表)。

该索引处的计数器递增,以保持该数字出现次数的计数。


然后,通过字典确定哪个位置是出现次数最多的数字,并返回该数字。

您可以首先对字符串进行排序,以便较小的数字字符首先出现在
num

声明变量以存储模式值(即,数据中最常出现的值)和频率

int mode=-1, modecount=-1, n;
现在找到每个数字字符的频率。由于这是一个排序字符数组,相同的值将连续出现

for(char *ptr=nums, *lptr=NULL; *ptr; ptr+=n)
{
    lptr = strrchr(ptr, *ptr);
    n = lptr - ptr + 1;
    if(n>modecount)
    {
        printf("\n%c", *ptr);
        modecount = n;
        mode = *ptr;
    }
}   
printf("\nMode is: %c", mode);

(从
string.h
)将查找字符串中最后出现的字符。

StackOverflow不是免费的编码服务。因此希望您这样做。请更新您的问题,以显示您在字符串中已尝试过的内容。有关更多信息,请参阅,并使用类似于字符频率程序的:)。初始化一个10位数的数组,循环使用
num
,然后找到最伟大的成员。@Barmar我用我目前掌握的内容更新了我的帖子,但我对指针的理解还不够深入。你能不用指针吗?对不起,在C语言中,如果不使用或不理解指针,就不能编写任何非平凡的程序。@Mulliganacile:现代编译器可能会对
strlen
函数进行假设,以内联方式实现它,并且可能能够确定您没有修改循环中的字符串,因此代码可能不会太慢,但是,您不应该依赖这些高级优化。根本不需要计算字符串的长度,只需测试
s[i]
是否为空终止符即可停止循环;而且我对这个也有点迷茫:*(count++*s-'0')++;你觉得你能澄清一点吗?对不起,我是C和指针的新手,非常感谢你的帮助。我确实需要更多的帮助来理解指针。你的第二个例子是不可编译的<代码>计数=calloc(sizeof(*计数),10)应该是
*count=calloc(sizeof(*count),10)@jwdonahue:抓得好!答案更新了,在单独的行中更具可读性,更不容易出错。我的意思是我不允许使用数组。不幸的是,我不能在代码中使用任何括号。这就是导致我出现问题的原因,因为我知道如何处理数组的问题。我不知道如何更好地描述我的问题,很抱歉,如果我没有很好地表达它,如果字符串有非数字字符,这段代码将出错。函数名
最常见的\u字符
意味着可能遇到任何字符。@30278560387在分配任务之前,他们没有告诉你
a[x]
等同于
*(a+x)
?您总是可以将任何数组表达式转换为这样的指针。在递增其元素之前,您需要初始化
freq
。@30278560387-好的,我添加了如何转换为代码以消除数组索引的示例。
strrchr(ptr,*ptr)!=NULL
似乎是多余的。您也可以使用嵌套循环:
f
while(*s)
  *(freq + (*s++ - '0')) = *(freq + (*s++ - '0')) + 1;
#include<string.h>    
int most (char* nums) {
    int i, max_index = 0;
    int digit_dictionary[10]={0,0,0,0,0,0,0,0,0,0};

    for (i=0; i< strlen(nums); i++) {
        digit_dictionary[nums[i]-'0'] ++;
    }

    for (i=1; i<10; i++) {
        if (digit_dictionary[i]> digit_dictionary[max_index])
            max_index = i;
    }
    return max_index;
}
int cmpfn(const void *a, const void *b)
{
    int x = *(char *)a;
    int y = *(char *)b;
    return x-y;
}
int main()
{
    char nums[] = "132433423";//"111222223333";//
    qsort(nums, sizeof(nums)/sizeof(nums[0]) -1, sizeof(nums[0]), cmpfn);
    printf("\nAfter sorting: %s", nums);

    . . . . . . . . . . 
    . . . . . . . . . . 

}
int mode=-1, modecount=-1, n;
for(char *ptr=nums, *lptr=NULL; *ptr; ptr+=n)
{
    lptr = strrchr(ptr, *ptr);
    n = lptr - ptr + 1;
    if(n>modecount)
    {
        printf("\n%c", *ptr);
        modecount = n;
        mode = *ptr;
    }
}   
printf("\nMode is: %c", mode);