Thread: flip

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    21

    flip


    how do you flip an int where by you enter an int exp:12345 and it outputs 54321?

    Thanx for the help

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You could do it mathematically I am sure, but more easy to do in c.
    Psuedo code
    convert to string,
    Use some string reverse function
    convert back to int.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	int number = 12345;
    	char buff[10], *p;
    	char reversed[10];
    
    	itoa(number,buff,10);
    	p = buff + strlen(buff)-1;
    
    	for(int i = 0; i < strlen(buff); i++)
    	{
    		reversed[i] = *p;
    		p--;
    	}
    	reversed[strlen(buff)] = 0;
    
    	printf("Original int: %d\nReversed int: %d\n",number,atoi(reversed));
    
    	return 0;
    }
    Last edited by bithub; 09-08-2004 at 10:53 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This one will work with (up to) 5-digit numbers anyway...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int num = 12345;
      int newnum = 0;
      int div;
    
      for(div = 1;div <= 10000;div *= 10)
        newnum += ((num/div)%10)*(10000/div);
      printf("%d\n", newnum);
    
      return 0;
    }
    itsme@dreams:~/C$ ./revnum
    54321
    Making it work with any size int is up to the OP to figure out.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh oh! Me too, me too!
    Code:
    #include <stdio.h>
    int main( void )
    {
        int  num = 12345, num2 = 0;
        while( num )
        {
            num2 *= 10;
            num2 += num%10;
            num /= 10;
        }
    
        printf("%d\n", num2 );
    
        for( num2 = 0, num = 12345; num; num2 *= 10, num2 += num%10, num /= 10);
    
        printf("%d\n", num2 );
    
    
        return 0;
    }
    Naturally, you could overflow your variable, but we have to leave you something to do on your own...

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    21
    Thanx for all the help.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >itoa(number,buff,10);
    itoa is not standard, sprintf works just as well and is more flexible.

    >for(int i = 0; i < strlen(buff); i++)
    If you're using C99 then say so to avoid being corrected. C89 does not allow variable declarations in a for loop. And calling strlen as the condition is an easily corrected performance issue.

    >printf("Original int: %d\nReversed int: %d\n",number,atoi(reversed));
    Do please include stdio.h. This is especially important with printf.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you're using C99 then say so to avoid being corrected. C89 does not allow variable declarations in a for loop. And calling strlen as the condition is an easily corrected performance issue.
    Yes, I'm using MSVC++. My goal here was not to write a highly optimized piece of perfection. I just wanted to give the question asker some ideas on how to solve his problem.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm using MSVC++.
    Then you aren't using C99, and the only way your code would compile is if you compile as C++, which means your code is wrong.

    >My goal here was not to write a highly optimized piece of perfection.
    Nor do I expect you to. I expect code that is 1) correct, 2) portable and 3) reasonably efficient. From what I see, your code fails all of those because C89 does not support variable declarations in a for loop, itoa is not a standard function, and calling strlen in a loop condition is a well known and EASILY fixed performance drain.

    >I just wanted to give the question asker some ideas on how to solve his problem.
    Until you can express your ideas correctly, I recommend you refrain from posting example code. You can give pseudocode, or just a description of what to do so that they can work their own way through the problem. Giving them poor code is worse than doing nothing at all because you're setting a bad example.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Great, another elitest messageboard programmer.

    which means your code is wrong
    My code was in not wrong, it just wasnt C. I admit I made the mistake of not paying close enough attention to what forum section I was in before posting. I will pay closer attention to that in the future.

    Until you can express your ideas correctly, I recommend you refrain from posting example code
    I dont appreciate your insinuation that my contributions to this board are unwelcome. If you look at my post history, you will see that I have helped several people with a myriad of problems over my short time being here. I have been programming in C/C++ for over 15 years now, and I enjoy sharing my knowledge with others. The last thing I want to hear from you is that I am hindering rather than helping others.
    Last edited by bithub; 09-09-2004 at 02:08 PM.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    My code was in not wrong, it just wasnt C.
    But it is wrong AS C code.
    True, but most compilers have itoa() as a built in extension.
    None of mine do
    I prefer to use it over sprintf() due to the fact that sprintf() doesnt check buffer sizes where itoa() does.
    No guartnees of that, since its non-standard there are no rules stating that it must check buffer sizes.
    I dont appreciate your insinuation that my contributions to this board are unwelcome. If you look at my post history, you will see that I have helped several people with a myriad of problems over my short time being here. I have been programming in C/C++ for over 15 years now, and I enjoy sharing my knowledge with others. The last thing I want to hear from you is that I am hindering rather than helping others.
    Prelude has been here a long time and is giving her opinon based on helping people for that amount of time. And some very practiced programmers still make dumb mistakes and write bad code.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    And some very practiced programmers still make dumb mistakes and write bad code.
    I will be the first to admit I still make dumb mistakes in my code. After 10 years coding as a hobby, and 5 years as a profession, I still find problems in my code. That wasnt my point though.

    Prelude has been here a long time and is giving her opinon based on helping people for that amount of time
    I respect Prelude for helping others in her free time. I just dont like how she jumped on my back for trying to do the same thing.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I admit I made the mistake of not paying close enough attention to what forum section I was in
    Fair enough. I do the same thing occasionally.

    >I dont appreciate your insinuation that my contributions to this board are unwelcome.
    I'm not responsible for how you choose to interpret what I say.

    >I just dont like how she jumped on my back for trying to do the same thing.
    Get over it. I (and many others) will let you know if you do someting wrong. Often very quickly, and we make no attempt at being nice about it because it's hard to criticize without sounding mean. I have nothing against you personally, I just try my best to ensure quality help for everyone. If that means I end up hurting feelings every now and then, so be it.

    >since its non-standard there are no rules stating that it must check buffer sizes.
    And generally it doesn't Just because you pass a buffer doesn't mean the boundaries are checked. Most implementations of itoa I've seen will overflow if the number is too large for the supplied buffer.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Christ this one is so simple you guys.

    Code:
    int i = 12345;
    printf ("%d", i % 10);
    while (i = i / 10) {
       printf ("%d", i % 10);
    }
    or something

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    That one won't work Brian. It cuts off the first digit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does memcpy cause endian flip?
    By Subsonics in forum C Programming
    Replies: 7
    Last Post: 01-17-2009, 06:30 PM
  2. code doesnt flip array
    By noob2c in forum C Programming
    Replies: 3
    Last Post: 03-29-2003, 09:54 AM
  3. Need help on flip array algorithm
    By DiepVien_007 in forum C Programming
    Replies: 4
    Last Post: 03-17-2003, 04:59 PM
  4. C++ algorithm help
    By Gene126 in forum C++ Programming
    Replies: 8
    Last Post: 10-10-2002, 03:17 PM
  5. flip
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-18-2002, 08:20 AM