how do you flip an int where by you enter an int exp:12345 and it outputs 54321?
Thanx for the help
how do you flip an int where by you enter an int exp:12345 and it outputs 54321?
Thanx for the help
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
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.
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; }Making it work with any size int is up to the OP to figure out.itsme@dreams:~/C$ ./revnum
54321
If you understand what you're doing, you're not learning anything.
Oh oh! Me too, me too!
Naturally, you could overflow your variable, but we have to leave you something to do on your own...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; }
Quzah.
Hope is the first step on the road to disappointment.
Thanx for all the help.
>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.
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.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.
>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.
Great, another elitest messageboard programmer.
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.which means your code is wrong
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.Until you can express your ideas correctly, I recommend you refrain from posting example code
Last edited by bithub; 09-09-2004 at 02:08 PM.
But it is wrong AS C code.My code was in not wrong, it just wasnt C.
None of mine doTrue, but most compilers have itoa() as a built in extension.
No guartnees of that, since its non-standard there are no rules stating that it must check buffer sizes.I prefer to use it over sprintf() due to the fact that sprintf() doesnt check buffer sizes where itoa() does.
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.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.
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.And some very practiced programmers still make dumb mistakes and write bad code.
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.Prelude has been here a long time and is giving her opinon based on helping people for that amount of time
>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.
Christ this one is so simple you guys.
or somethingCode:int i = 12345; printf ("%d", i % 10); while (i = i / 10) { printf ("%d", i % 10); }
That one won't work Brian. It cuts off the first digit.