C语言Matrix编程题——[Pointers and C-String]D. Liang 7.10 Occurrences of each digit in a string

[Pointers and C-String]D. Liang 7.10 Occurrences of each digit in a string

Description:

Write a function that counts the occurrences of each digit in a string using the following header:

int * count(const char * const s);

The function counts how many times a digits appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int counts[] = count("12203AB3"), counts[0]is 1, counts[1] is 1, counts[2]is 2, counts[3]is 2.

Hint:

Don’t submit the main() function.

Programme:

//Date:2020/5/18
//Author:Kamenrider Justice
int * count(const char * const s)
{
   int i,length,*count;
   count=(int*)malloc(10*sizeof(int));//分配内存
   for(i=0;i<10;i++)//初始化为0
   {
      count[i]=0;
   }
   length=strlen(s);
   for(i=0;i<length;i++)
   {
      switch(s[i])//采用switch语句
      {
         case '0':
         {
            count[0]++;
            break;
         }
         case '1':
         {
            count[1]++;
            break;
         }
         case '2':
         {
            count[2]++;
            break;
         }
         case '3':
         {
            count[3]++;
            break;
         }
         case '4':
         {
            count[4]++;
            break;
         }
         case '5':
         {
            count[5]++;
            break;
         }
         case '6':
         {
            count[6]++;
            break;
         }
         case '7':
         {
            count[7]++;
            break;
         }
         case '8':
         {
            count[8]++;
            break;
         }
         case '9':
         {
            count[9]++;
            break;
         }
         default:
         {
            break;
         }
      }
   }
   return count;
}

数据结构核心原理与算法应用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
以下是使用C语言编写的程序,可以使用函数的call by value和call by reference两种方式来计算一个数的平方: ```c #include <stdio.h> // Call by value function to find square of a number int squareByValue(int num) { return num * num; } // Call by reference function to find square of a number void squareByReference(int *numPtr) { *numPtr = (*numPtr) * (*numPtr); } int main() { int num = 5; // Call by value example int square1 = squareByValue(num); printf("Square of %d is %d (Call by value)\n", num, square1); // Call by reference example squareByReference(&num); printf("Square of %d is %d (Call by reference)\n", num, num); return 0; } ``` 该程序定义了两个函数,一个是使用call by value方式计算平方的函数,另一个是使用call by reference方式计算平方的函数。在主函数中,我们首先定义一个整数num,并将其设置为5。然后,我们使用call by value方式调用squareByValue函数来计算num的平方,并将结果存储在square1变量中。接下来,我们使用call by reference方式调用squareByReference函数来计算num的平方,这里我们传递了num的指针作为参数。最后,我们打印出计算结果。 运行该程序,输出结果为: ``` Square of 5 is 25 (Call by value) Square of 25 is 25 (Call by reference) ``` 从输出结果可以看出,两种方式都可以正确地计算num的平方。使用call by value方式时,我们将num的值作为参数传递给函数,并在函数中进行计算。使用call by reference方式时,我们将num的地址作为参数传递给函数,并在函数中使用指针来访问num的值并进行计算。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值