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

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I said divide by 10 repeatedly. What will the last non-zero result of doing that be?

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's because your code isn't quite doing what I suggested....

  7. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Divide by 10 until the result itself is less than 10 - then you have the first digit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use another variable man

    Code:
    int last = mynumber % 10;
    int first = mynumber;
    while ( first > 10 ) { first = first / 10; }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ah so that was it. What was wrong with the variable name by the way?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use any variable names you like, so long as it's a meaningful name in the context which you use it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double linked list
    By kukluksklanas in forum C Programming
    Replies: 4
    Last Post: 03-10-2009, 11:15 AM
  2. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 01:53 PM
  3. Read .txt file into 2D array.
    By crazygopedder in forum C Programming
    Replies: 11
    Last Post: 10-21-2008, 08:42 PM
  4. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  5. Problem ending loop
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2006, 12:43 PM