Home All Groups Group Topic Archive Search About

Increase all digits in a number(string) one step

Author
10 Mar 2006 3:12 PM
Niklas Engfelt
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

Author
10 Mar 2006 4:03 PM
Jon Skeet [C# MVP]
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
Are all your drivers up to date? click for free checkup

Author
12 Mar 2006 9:02 AM
Nick Malik [Microsoft]
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.

--
--- 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.
--
Show quoteHide quote
"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
>
Author
12 Mar 2006 4:11 PM
Jon Skeet [C# MVP]
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
> 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.

I do generally - I didn't think of this question as homework. In
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
Author
12 Mar 2006 11:48 PM
Otis Mukinfus
On Sun, 12 Mar 2006 01:02:22 -0800, "Nick Malik [Microsoft]"
<nickmalik@hotmail.nospam.com> wrote:

Show quoteHide quote
>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.
>
>--
>--- 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.

A search of Google reveals Mr. Engfelt is interested in many things and does not
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
Author
13 Mar 2006 9:18 AM
Niklas Engfelt
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.
Author
14 Mar 2006 11:56 AM
Niklas Engfelt
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
Author
10 Mar 2006 4:04 PM
Red2
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
Author
10 Mar 2006 4:05 PM
Red2
yeah, what he said!
Author
10 Mar 2006 4:25 PM
Ignacio Machin ( .NET/ C# MVP )
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
>
Author
10 Mar 2006 6:32 PM
Mattias Sjögren
>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?

Here's a numeric version:

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.

Bookmark and Share