[Function Question 8] PTA #C

Question

  1. We will write a function Count the number of occurrences of a particular number in a string of numbers
  2. which looks like: int CountDigit( int number, int digit );
  3. The number we input should be contained by the “long” type.
  4. The ‘digit’ should be an integer number in range of [0,9]

Test Samples

#include <stdio.h>

int CountDigit( int number, int digit );

int main()
{
    int number, digit;

    scanf("%d %d", &number, &digit);
    printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));

    return 0;
}

we will add our functions at the end of the code

Input Samples

-21252 2

Output Samples

Number of digit 2 in -21252: 3

Note Source of the question: 浙大版《C语言程序设计(第3版)》题目集

Ideas to solve the question

We can see from the Output Samples that we will only put the number we count at the end of the function.
To solve this question, we can iterate the whole number and compare every number with the ‘digit’.
There are two method to reach that, one way is get the remainder int the loop to get every number, another idea is that we can change the type of the number, we will change it into a char type instead of the integer, and in the loop we compare the ASCII code of the number and the digit
But we will use the itoa function in the <stdlib.h> library, we can see the head of the code, we do not include this library, so method seems can not use in this question.

int CountDigit( int number, int digit)
{
  int result = 0;
  int element;
  
  while (number != 0) {
    element = number % 10;
    if (digit == element) {
      result += 1;
    }
    number = number / 10;
  }
  return result;
}

we define the element variable to get every piece of number in tnumber, and compare it with the digit variable.
You might also notice that I use a while loop but not a for loop, because we do not know the length of the number, and we will reduce the loop by divide the numbver by 10 repeatly until it become to 0.

we can run it…

Number of digit 2 in -21252: 3

And we get it!

So the whole code looke like that:

#include <stdio.h>

int CountDigit( int number, int digit );

int main()
{
    int number, digit;

    scanf("%d %d", &number, &digit);
    printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));

    return 0;
}


int CountDigit( int number, int digit)
{
  int result = 0;
  int element;
  if (number < 0) {
    number = - number;
  }
  while (number != 0) {
    element = number % 10;
    if (digit == element) {
      result += 1;
    }
    number = number / 10;
  }
  return result;
}

Some ways to make it better…

As we mentioned the second way (change it into char type and compare it)
I slao get the code but there are some small bugs about change the digit variablre into char type.

#include <stdio.h>
#include <stdlib.h>

int CountDigit( int number, int digit );

int main()
{
   int number, digit;

   scanf("%d %d", &number, &digit);
   printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));

   return 0;
}

int CountDigit( int number, int digit)
{
  int result = 0;
  char string[16] = {0};
  char Cdigit[16] = {0};
  itoa(number,string,10);
  itoa(digit,Cdigit,10);
  for (int i=0;i<16;i++) {
    if (string[i] == Cdigit[0]) {
      result += 1;
    }
  }

}

When I run it, it will looks like:
input:

-21252 2

output:

Number of digit 2 in -21252: 50

How could it be 50???
can somebody help me please?
And hope the code above can help you!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值