I have account numbers and the leading zero's have been dropped. For example 01234567 is instead recorded as 1234567. How can I easily put all the zero's back in?
Thanks for the help.
You can use the Z format to print a number with leading zeros; you'd want z8. to print 8 digits with leading zeros as necessary.
A bigger problem is that your data is probably the wrong type. It's generally a bad idea to store things like phone numbers and account numbers as numerics. You should only store something as a numeric if you plan to do arithmetic with it. Anything else should be a character variable, even if it only consists of the digits 0-9.
If possible, create the variable as a character variable from the start. That will preserve the leading zeros and also prevent multiple possible headaches in the future.
Related
Have to make program, where you input number, and program outputs most repeated digit in it, can't figure out have to do it. Tried some things, for me it works with static array, but I need dynamic, so I dont now what to do.
Can someone help me?
make an array in size of 10 (the number of digits)
run over your original array and extract from each number all its digit, increase the value of your digit array for each digit you find.
-find the maximal digit using the sight array.
You can upload a code if you want more help
I assume you are served in real time digits [0-9] and you need a function that at any given time returns you the most frequent digit seen so far. A simplest solution would be just to have a hash map of [0-9] keys that maintains the number of times every digit is seen. When you need the most frequent digit you iterate over the 10 keys and return the one with the biggest count.
Strings will always end with 'Row' followed by a number. For example,
desk_Row2.txt
desk_Row15.txt
If sorted, desk_Row15.txt will precede desk_Row2.txt.
If it's a single digit number, I want to put a leading 0 in front of it so that, when sorted, it will be:
desk_Row02.txt
desk_Row15.txt
I figured out a long way where I find 'Row' with findstr and '.' and what's between them is a number. Then I can figure out whether str2double(that) is greater than 9. Well, I think this can be done in a matter of one or two sentences.
More generally, I want to learn to create expressions so that I can do the above myself later on. For example, I have no idea what (^|\.)\s*. means.
I was thinking to use regexprep, but I have no idea what the expression should be.
I read an integer number :
is >> myInteger;
Now I want to know how many digits were read (I'm talking of the possible leading zeros). How can I do that?
You can:
get the value as a string, then parse it separately, however you wish (check length, count zeros, etc).
use is.tellg for this; Keep in mind that tellg will give you buffer positions, not not what was at those positions (it could be space characters or zeros)
read the buffer character by character using is.get, then process values according to your needs.
You could get the value of is.tellg() before you stream in the integer, then get it again and find the difference.
EDIT: Although as pointed out in the comments that will just tell you how many elements of the stream were consumed, some of which may be whitespace.
Hey I'm supposed to develop a regular expression for a binary string that has no consecutive 0s and no consecutive 1s. However this question is proving quite tricky. I'm not quite sure how to approach it as is.
If anyone could help that'd be great! This is new to me.
You're basically looking for alternating digits, the string:
...01010101010101...
but one that doesn't go infinitely in either direction.
That would be an optional 0 followed by any number of 10 sets followed by an optional 1:
^0?(10)*1?$
The (10)* (group) gives you as many of the alternating digits as you need and the optional edge characters allow you to start/stop with a half-group.
Keep in mind that also allows an empty string which may not be what you want, though you could argue that's still a binary string with no consecutive identical digits. If you need it to have a length of at least one, you can do that with a more complicated "or" regex like:
^(0(10)*1?)|(1(01)*0?)$
which makes the first digit (either 1 or 0) non-optional and adjusts the following sequences accordingly for the two cases.
But a simpler solution may be better if it's allowed - just ensure it has a length greater than zero before doing the regex check.
I've got a string value of the form 10123X123456 where 10 is the year, 123 is the day number within the year, and the rest is unique system-generated stuff. Under certain circumstances, I need to add 400 to the day number, so that the number above, for example, would become 10523X123456.
My first idea was to substring those three characters, convert them to an integer, add 400 to it, convert them back to a string and then call replace on the original string. That works.
But then it occurred to me that the only character I actually need to change is the third one, and that the original value would always be 0-3, so there would never be any "carrying" problems. It further occurred to me that the ASCII code points for the numbers are consecutive, so adding the number 4 to the character "0", for example, would result in "4", and so forth. So that's what I ended up doing.
My question is, is there any reason that won't always work? I generally avoid "ASCII arithmetic" on the grounds that it's not cross-platform or internationalization friendly. But it seems reasonable to assume that the code points for numbers will always be sequential, i.e., "4" will always be 1 more than "3". Anybody see any problem with this reasoning?
Here's the code.
string input = "10123X123456";
input[2] += 4;
//Output should be 10523X123456
From the C++ standard, section 2.2.3:
In both the source and execution basic character sets, the value of each character after 0 in the
above list of decimal digits shall be one greater than the value of the previous.
So yes, if you're guaranteed to never need a carry, you're good to go.
The C++ language definition requres that the code-point values of the numerals be consecutive. Therefore, ASCII Arithmetic is perfectly acceptable.
Always keep in mind that if this is generated by something that you do not entirely control (such as users and third-party system), that something can and will go wrong with it. (Check out Murphy's laws)
So I think you should at least put on some validations before doing so.
It sounds like altering the string as you describe is easier than parsing the number out in the first place. So if your algorithm works (and it certainly does what you describe), I wouldn't consider it premature optimization.
Of course, after you add 400, it's no longer a day number, so you couldn't apply this process recursively.
And, <obligatory Year 2100 warning>.
Very long time ago I saw some x86 processor instructions for ASCII and BCD.
Those are AAA (ASCII Adjust for Addition), AAS (subtraction), AAM (mult), AAD (div).
But even if you are not sure about target platform you can refer to specification of characters set you are using and I guess you'll find that first 127 characters of ASCII is always have the same meaning for all characters set (for unicode that is first characters page).