0

I'm having some troubles... I'm trying to input a number, followed by an integer of multiple numbers. I am trying to count how many times the first number occurs in the integer. Now, I made this really easy code to show you what I actually am trying to do. The thing is, this code only compares two integers and tells me if they are the same or not. Mind you, I am very unexperienced in C programming, hence this question...

int main(){

    int numberOne;
    int numberTwo;
    int count = 0;

    scanf("%d", &numberOne);
    scanf("%d", &numberTwo);

    if(numberOne == numberTwo){
        count++;
    }

    printf("Amount of equals found: %d", count);

return 0;
}

Now, if I'd have the following input: '1 1021023234', the output would be: 'Amount of equals found:0' The output should be (in this case) 'The output would be Amount of equals found:2'

I hope you guys can give me some tips.

4 Answers4

1

If I have understood correctly then you need the following

#include <stdio.h>

int main(void) 
{
    unsigned int numberOne;
    unsigned int numberTwo;
    unsigned int x;
    unsigned int base;
    size_t n;

    printf( "Enter first number: " );
    scanf( "%u", &numberOne );

    printf( "Enter second number: " );
    scanf( "%u", &numberTwo );

    x = numberOne;
    base = 1;
    do { base *= 10; } while ( x /= 10 );

    n = 0;

    do { n += numberOne == numberTwo % base; } while ( numberTwo /= 10 );

    printf( "Amount of equals found: %u", n );

    return 0;
}

For numbers 12 and 76512612 the output is

Amount of equals found: 2
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Write the code yourself. Here are the hints:

Since you need only one input,eliminate numbertwo from your program and implement the following:

  1. numberone%10 would give the first digit of the user input. Assign it to a variable one.

  2. If user input(numberone)<10 , print first digit is found 1 time in input and exit the program.

  3. Use a while loop and put numberone>=10 in its condition.check if(numberone%10==one) in the loop if true,increment variable count. Add numberone/10 after it.

  4. Print your last printf and end the program

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

you can read them as integer but you cannot compare them the way you are doing. to compare them you need to extract each digit from the number and compare.

temp = numbertwo % 10; //this gives the digit in the unit place
if(temp == numberone)
    count++;
numbertwo = numbertwo / 10; //this now removes the last digit that has already been compared

keep the above code in a loop until numbertwo becomes zero. and it will work as you wanted

another way to do this would be to read the integer as a String.

char* numbertwo;
char = malloc(20);
fgets(numbertwo, 20, stdin);

and then compare the integer in a for loop

Haris
  • 12,120
  • 6
  • 43
  • 70
0

You can try it using strings as below..

#include<stdio.h>
#include<string.h>
main()
{
char int1[100];
char int2[100];
int cnt=0;
scanf("%s",int1);
scanf("%s",int2);
char *p;
while(p=strstr(s1,s2))   //searches for the string s2 in s1 and if found, returns the address to the pointer p//
{
cnt++
s1=p+strlen(s2);   //now s1 points to the next location from the end of string s2 in s1//
}
printf("Amout of equals found:%d\n",cnt");
}