# Thread: Is there anyway to check if an int has the same beginning and ending?

1. ## Is there anyway to check if an int has the same beginning and ending?

Java has indexof, is there anything like that in C for ints? for example if i have 233 i want to check if the first number 2 matches the last 3.
An array could be used of course and by putting the number inside it and comparing the first and third element, but is there a way without it?

2. Sure. Work out a way to extract the first and large digits. One way is to print the integer to a string (e.g. using itoa()), strip off any whitespace, and then compare the first digit printed with the last one. There are other ways (eg using value%10 to find the last digit, and dividing by 10 repeatedly to find the first).

3. thanks, value%10 seems to work fine but dividing by 10 works for 2 characters but it doesn't for 2 or greater numbers.

Code:
int first;
int stop = 1;
if (stop == 1)
{
first = number/10;
printf("\n%s", "FIRST ");
printf("%d", first, "\n");
stop++;

}
number entered: 12

first number is: 1

number entered: 123

first number is: 12

4. I said divide by 10 repeatedly. What will the last non-zero result of doing that be?

5. hmm same result:

Code:
int temp2 = 0;
while (temp2 < number)
{
first = number/10; // number = number being entered
printf("\n%s", "FIRST ");
printf("%d", first, "\n");
temp2++;

}

6. That's because your code isn't quite doing what I suggested....

7. i don't quiet understand, it is... you suggested that i divide by 10 repeatedly so i don't get what i'm doing wrong?

8. Divide by 10 until the result itself is less than 10 - then you have the first digit

9. Hmm where will the result come from and should i divide the number that's being entered or is it something else (just making sure im not doing the wrong thing)?

10. Use another variable man

Code:
int last = mynumber % 10;
int first = mynumber;
while ( first > 10 ) { first = first / 10; }

11. ah so that was it. What was wrong with the variable name by the way?

12. Use any variable names you like, so long as it's a meaningful name in the context which you use it.