473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Increase all digits in a number(string) one step

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

Mar 10 '06 #1
10 2312
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

Mar 10 '06 #2
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

Mar 10 '06 #3
yeah, what he said!

Mar 10 '06 #4
Hi,

this sounds like a homework :)
"Niklas Engfelt" <en*****@gmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.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

Mar 10 '06 #5
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.
Mar 10 '06 #6
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.
--
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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

Mar 12 '06 #7
Nick Malik [Microsoft] <ni*******@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
Mar 12 '06 #8
On Sun, 12 Mar 2006 01:02:22 -0800, "Nick Malik [Microsoft]"
<ni*******@hotmail.nospam.com> wrote:
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
Mar 13 '06 #9
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.

Mar 13 '06 #10
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

Mar 14 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Westcoast Sheri | last post by:
What is the most efficient way of extracting the first two digits in a string? The following is wrong for me, because it only gives me the first instance of two digits together: $string =...
11
by: John Velman | last post by:
I've used perl for a lot of 'throw away' scripts; I like Python better in principle, from reading about it, but it was always easier to just use perl rather than learn python. Now I'm writing a...
19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
0
by: Gianluca | last post by:
Anyone knows some open source code to parse date and number strings? The parse functions in the .NET framework are buggy and cannot test for the validity of the string without throwing an...
3
by: Tracey | last post by:
I'm learning C++ and was stuck when writing code to convert a negative decimal number string to a hexdecimal number. For example, suppose the function interface is defined as: int atoi( const...
7
by: jack | last post by:
Hello, I need to get the time down the the 0.1 second in a number string. Example 09-06-03 13:45 23.4 Seconds To "0906031345234"
3
by: Jim Tittsler | last post by:
Is there a standard recipe for getting the subversion revision number into my Python-based application each time I package it up with distutils? (Not just the package name, but also a string that...
6
eyeofsoul
by: eyeofsoul | last post by:
how do i increase a string into default lenght? for example i put a string, java.how to transform the string to for example 24 character in it?
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.