Home > Back-end >  finding how many times number2 is showing in number1
finding how many times number2 is showing in number1

Time:12-02

Hi i am solving an exercise in c, and i got stuck i don't know the logic of the code to get to my solution. For example we enter 2 numbers from input let the numbers be 123451289 12 and i want to see how many times number 2 is showing at number 1(if this is confusing let me know). For the numbers earlier the program outputs 2. I tried solving it here is my code:

#include <stdio.h>
int main(){
    int num1,num2,counter=0;
    scanf("%d%d",num1,num2);
    if(num1<num2){
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }
    int copy1 = num1;
    int copy2 = num2;
    while (copy2>0) {
        counter  ; // GETTING THE LENGHT OF THE SECOND NUMBER
        copy2/=10;
//        lastdigits = copy1%counter //HERE I WANT TO GET THE LAST DIGITS OF THE FIRST NUMBER
// But it does not work
    }

}

My question is how can i get the last digits of the first number according to the second one for example if the second number have 3 digits i want to get the last 3 digits of the first number. For the other part i think i can figure it out.
WITHOUT USING ARRAYS

CodePudding user response:

The problem: find all the needles (e.g. 12) in a haystack (e.g. 123451289).

This can be done simply without arrays using a modulus of the needle. For 12, this is 100. That is, 12 is two digits wide. Using the modulus, we can isolate the rightmost N digits of the haystack and compare them against the needle.

We "scan" haystack repeatedly by dividing by 10 until we reach zero.

Here is the code:

#include <stdio.h>

int
main(void)
{
    int need, hay, counter = 0;

    scanf(" %d %d", &hay, &need);

    // ensure that the numbers are _not_ reversed
    if (hay < need) {
        int temp = need;
        need = hay;
        hay = temp;
    }

    // get modulus for needle (similar to number of digits)
    int mod = 1;
    for (int copy = need;  copy != 0;  copy /= 10)
        mod *= 10;

    // search haystack for occurences of needle
    // examine the rightmost "mod" digits of haystack and check for match
    // reduce haystack digit by digit
    for (int copy = hay;  copy != 0;  copy /= 10) {
        if ((copy % mod) == need)
              counter;
    }

    printf("%d appears in %d exactly %d times\n",need,hay,counter);

    return 0;
}

CodePudding user response:

This looks along the lines of what you're attempting.

You can use the pow() function from math.h to raise 10 to the power of how many digits you need for your modulus operation.

Compile with -lm or make your own function to calculate 10^num_digits

#include <stdio.h>
#include <math.h>

int main() {
    int x = 123456789;
    double num_digits = 3.0;
    int last_digits = x % (int)pow(10.0, num_digits);
    printf("x = %d\nLast %d Digits of x = %d\n", x, (int)num_digits, last_digits);

    return 0;
}

Outputs:

x = 123456789
Last 3 Digits of x = 789

CodePudding user response:

I think you are trying to ask :- if number1 = 1234567 and number2 = 673, then, length of number2 or number2 has 3 digits, so, you now want the last 3 digits in number1, i.e, '456', if I'm not wrong.

If that is the case, then, what you did to find the number of digits in num2 is correct, i.e,

while (copy2>0) {
        counter  ; // GETTING THE LENGHT OF THE SECOND NUMBER
        copy2/=10;
}

you can do the same for number1 and find out its number of digits, then you can compare whether the number of digits in number2 is less than that in number1. Ex, 3 is less than number of digits in number1, so you can proceed further. Let's say number of digits in number1 is 7 and you want the last 3 digits, so you can do iterate over the digits in number1 till count of digits in number2 and pop out each last digit and store them in an array.

The code:

#include <stdio.h>

int main()
{
    
    int num1,num2;
    
    int count1 = 0, count2 = 0;
    
    scanf("%d",&num1);
    scanf("%d",&num2);
    
    if(num1<num2){
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }
    
    int copy1 = num1;
    int copy2 = num2;
    
    while (copy1>0)
    {
        count1  ;
        copy1/=10;
    }
    
    while (copy2>0)
    {
        count2  ;
        copy2/=10;
    }
    
    // printf("num1 has %d digits and num2 has %d digits\n", count1, count2);
    
    if (count1 >= count2)
    {
        int arr[count2];
        int x = count2;
        int p = num1;
        int i = 0;
        
        while (x > 0)
        {
            arr[i  ] = p%10;
            x --;
            p/=10;
        }
        
        for (int j = 0; j < i; j  )
        {
            printf("%d ", arr[j]);
        }
    }
    
    return 0;
}

output : 8 7 6

let's say, num1 = 12345678, num2 = 158, then arr = {8,7,6}.

CodePudding user response:

This was the answer that i was looking for but thank you all for helping :)

#include <stdio.h>
#include <math.h>
int main(){
    int num1,counter1,counter2,num2,temp,digit,copy1,copy2;
    scanf("%d%d",&num1,&num2);
    if(num1<num2){
        temp = num1;
        num1 = num2;
        num2 = temp;
    }
    copy1 = num1;
    copy2 = num2;
    counter1 = counter2 = 0;
    while (copy2>0) {
        counter1  ;
        copy2/=10;
    }
    counter1 = pow(10,counter1);
    while (copy1>0) {
        digit = copy1%counter1;
        if(digit==num2){
            counter2  ;
        }
        copy1/=10;
    }
    printf("%d",counter2);
}
  •  Tags:  
  • c
  • Related