|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Increase all digits in a number(string) one stepDoes anyone have a nice algorithm/method to increase all digits one
step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? Best regards Niklas Niklas Engfelt wrote:
> Does anyone have a nice algorithm/method to increase all digits one Well, it depends what you mean by "in one step". In one method call?> step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? Very easily: static string IncDigits(string text) { char[] chars = text.ToCharArray(); for (int i=0; i < chars.Length; i++) { char c = chars[i]; if (c >= '0' && c <='9') { // Not just c++ - wrap 9->0 c = (char)((((c-'0')+1)%10)+'0'); chars[i]=c; } } return new string(chars); } Jon Hi Jon,
You are a tremendous help to all who come to this board, and I am always in awe of the completeness and clarity by which you communicate some very technical things. That said, we do no student any favors by doing their homework for them. We also make it difficult for teachers, since that particular problem can never be used again for homework (thanks to the power of Google). As the son of two college professors, I come to you as a colleague to beg of you to set an excellent example for this board and to do your best to avoid helping a student to cheat on their homework. -- Show quoteHide quote--- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:1142004744.989115.288520@i39g2000cwa.googlegroups.com... > Niklas Engfelt wrote: >> Does anyone have a nice algorithm/method to increase all digits one >> step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? > > Well, it depends what you mean by "in one step". In one method call? > Very easily: > > static string IncDigits(string text) > { > char[] chars = text.ToCharArray(); > > for (int i=0; i < chars.Length; i++) > { > char c = chars[i]; > if (c >= '0' && c <='9') > { > // Not just c++ - wrap 9->0 > c = (char)((((c-'0')+1)%10)+'0'); > chars[i]=c; > } > } > return new string(chars); > } > > Jon > Nick Malik [Microsoft] <nickmalik@hotmail.nospam.com> wrote:
> You are a tremendous help to all who come to this board, and I am always in I do generally - I didn't think of this question as homework. In > awe of the completeness and clarity by which you communicate some very > technical things. That said, we do no student any favors by doing their > homework for them. We also make it difficult for teachers, since that > particular problem can never be used again for homework (thanks to the power > of Google). As the son of two college professors, I come to you as a > colleague to beg of you to set an excellent example for this board and to do > your best to avoid helping a student to cheat on their homework. retrospect, it may well be, in which case I apologise. I quite agree that doing students' homework for them isn't a good idea. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too On Sun, 12 Mar 2006 01:02:22 -0800, "Nick Malik [Microsoft]"
<nickmalik@hotmail.nospam.com> wrote: Show quoteHide quote >Hi Jon, A search of Google reveals Mr. Engfelt is interested in many things and does not> >You are a tremendous help to all who come to this board, and I am always in >awe of the completeness and clarity by which you communicate some very >technical things. That said, we do no student any favors by doing their >homework for them. We also make it difficult for teachers, since that >particular problem can never be used again for homework (thanks to the power >of Google). As the son of two college professors, I come to you as a >colleague to beg of you to set an excellent example for this board and to do >your best to avoid helping a student to cheat on their homework. > >-- >--- Nick Malik [Microsoft] > MCSD, CFPS, Certified Scrummaster > http://blogs.msdn.com/nickmalik > >Disclaimer: Opinions expressed in this forum are my own, and not >representative of my employer. > I do not answer questions on behalf of my employer. I'm just a >programmer helping programmers. appear to be a student, although I thought the same when I saw the post... Is the question he asked one of the certification question? Otis Mukinfus http://www.arltex.com http://www.tomchilders.com Nick, Ignacio:
I am not a student. Do you homework and do a google before commenting like that. ;-) I work mostly with MS products (BizTalk, C#, .NET, VB, SQL, Windows etc) and just wanted to find out a way to scramble/unscramble some numeric info that I store, but is not too sensitive, and I wanted to learn the best way to do that. (I enjoy studying although I work now ;-) Thanks alot for the info you have come up with! I will try those solutions. Jon:
Thanks! :) Your function does the job superbly without having to "cheat" with the if('9')... which I probably would have made. I am not too familiar with the %-operator yet. I guess it is time to look at that... /Niklas Can you convert using .ToCharArray(), loop through the char array 1 at
a time, convert to int, add 1 convert back. Finally convert back the whole char array to a string? Just brain dumping Hi,
this sounds like a homework :) Show quoteHide quote "Niklas Engfelt" <engf***@gmail.com> wrote in message news:1142003521.247793.95240@z34g2000cwc.googlegroups.com... > Does anyone have a nice algorithm/method to increase all digits one > step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? > > Best regards > Niklas > >Does anyone have a nice algorithm/method to increase all digits one Here's a numeric version:>step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? uint IncDigits(uint num) { uint m = 1; do { if ((num / m) % 10 == 9) num -= 9 * m; else num += m; } while ((m *= 10) <= num); return num; } Of course since it's dealing with numbers and not strings/chars, leading zeros will be lost, so I think Jon's version is more what you're looking for. Plus it will overflow for numbers >= 1e9, so it doesn't handle the whole range up to UInt32.MaxValue. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Other interesting topics
Anonymous Methods As ThreadStarts
newbie: GetType or typeof? How to Anonymous IP address in C# string dictionary and memory issue. Sockets, to be continued... What is wrong with this Producer-Consumer sample? file uploading in windows application in .net Desing problem Using a Class Library Settings File in ASP.NET 2.0 Regular Expresions |
|||||||||||||||||||||||