473,402 Members | 2,072 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,402 software developers and data experts.

remove dash in ssn with sprintf

Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?

Aug 9 '06 #1
19 3782
Carson wrote:
Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?
My golden hammer for this kind of thing is regex. But if the data is
guaranteed to be consistent, I'd probably just do it with a pointer and
strchr(). I don't see how you'd do this with "just" sprintf. If you
had the 3 numeric parts and you wanted to put dashes *in*, it would be
really easy to do with sprintf.

Maybe a job for sscanf, or strtok.
Aug 9 '06 #2
Carson <c3***@yahoo.comwrote:
Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?
*scanf() can do the trick:

#include <stdio.h>

int main(void) {
char test[] = "123-45-6789";
char test2[sizeof test];
sscanf(test,"%3s-%2s-%4s",test2,test2+3,test2+5);
printf("%s\n",test2);
return 0;
}

(Assuming, of course, that the string is in a known format. Error
checking and the like omitted.)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 9 '06 #3
"Carson" <c3***@yahoo.comwrites:
Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?
I assume the SSN is stored as a string. You can copy the string to
another string, copying only the characters other than '-'. As you're
doing this, you'll need to indices, one into the source string and
another into the target string. This is likely to be easier, and
probably faster, than any method using sprintf() and/or functions from
<string.h>.

Since you're shortening the string, you could even do this in place,
but only if the original string is writable *and* you don't mind
clobbering it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 9 '06 #4

Christopher Benson-Manica wrote:
#include <stdio.h>

int main(void) {
char test[] = "123-45-6789";
char test2[sizeof test];
sscanf(test,"%3s-%2s-%4s",test2,test2+3,test2+5);
printf("%s\n",test2);
return 0;
}
Is sscanf guaranteed to work "from left to right"?
Because if it is not, then the result could be something like
'1' '2' '3' '\0' '5' '\0' '7' '8' '9' '\0'
Just wondering.

Aug 9 '06 #5
Carson wrote:
Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?
1. Stripping dashes out of ssn's is usually a *very* bad idea,
unless there is a very specific reason to do so, and it is
not being stored in a database.

Often, you also have to deal with work-permit ID numbers
too, which has an entirely different format in the dashes.

2. sprintf could do it, but there are better (faster) ways.
just use "%-3.3s%-2.2s%-4.4s", and pass the ssn to the sprintf
function (three times, with appropriate '+n' on 2nd and later
call).

3. If you know exactly where the dashes are, strncpy would be
much faster.

4, If you don't know where they are, loop through the array,
copying anything that ain't a dash.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 9 '06 #6
my**************@gmail.com wrote:
Is sscanf guaranteed to work "from left to right"?
7.19.6.2 of n869 says, in part:

4. The fscanf function executes each directive of the format in turn.

7. A directive that is a conversion specification defines a set of
matching input sequences, as described below for each specifier.
A conversion specification is executed in the following steps:

8. Input white-space characters are skipped.

9. An input item is read from the stream.

10. The input item is converted to a type appropriate to the
conversion specifier. [T]he result of the conversion is placed in the
object pointed to by the first argument following the format argument
that has not already received a conversion result.

The answer to your question is "yes", as far as I can tell.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 9 '06 #7

Carson wrote:
Is it possible to remove the dashes in a social security number using
sprintf?
No, at least not portably and reliably. Depending on details of the
source format and required result format, you could use sprintf to
produce a copy of the SSN without dashes though.
If so what would the syntax look like?
Something along the lines of the following untested snippets would do
the trick:

char result[10];
char *ssn = "123-45-6789";

sprintf(result, "%.3s%.2s%.4s", ssn, ssn+4, ssn+7);

This assumes your source is a string, you know for certain it is in the
correct format, and you want the result to be a string. In that case I
might just do it this way. More likely I'd need to verify the source
string as part of the exercise, and it would probably be easier to
extract the required information while verifying the format.
If sprintf won't do this is there a C function that will?
There are many ways of doing it. Which is most appropriate depends on
precisely what you are trying to do, what your constraints are, what
guarantees you have about the data you're starting with, and what
formats and types of objects you're working with; none of which you
specify.

Aug 9 '06 #8
my**************@gmail.com schrieb:
Christopher Benson-Manica wrote:
>>#include <stdio.h>

int main(void) {
char test[] = "123-45-6789";
char test2[sizeof test];
sscanf(test,"%3s-%2s-%4s",test2,test2+3,test2+5);
printf("%s\n",test2);
return 0;
}

Is sscanf guaranteed to work "from left to right"?
Yes. If one of the conversions does not work, sscanf() will
terminate early (and return the number of successful conversions);
as always,
if (3 != sscanf(....)) {
/* Handle error */
}
is a better way of using sscanf().
Because if it is not, then the result could be something like
'1' '2' '3' '\0' '5' '\0' '7' '8' '9' '\0'
If you think it clearer, you can of course use %c for the first
two conversions.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 9 '06 #9

Kevin Handy wrote:
Carson wrote:
Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?

1. Stripping dashes out of ssn's is usually a *very* bad idea,
unless there is a very specific reason to do so, and it is
not being stored in a database.
I'm trying to create a magnetic media file for W2's which needs to have
the dashes removed from the ssn's.

2. sprintf could do it, but there are better (faster) ways.
just use "%-3.3s%-2.2s%-4.4s", and pass the ssn to the sprintf
function (three times, with appropriate '+n' on 2nd and later
call).

3. If you know exactly where the dashes are, strncpy would be
much faster.
I do know exactly where the dashes are. The database column is
char(11) and the dashes are in positions 4 and 7. What is the syntax
used with strncpy, 'man strncpy' didn't give much info.

strncpy(variable, $table.socsecno, <what do I put here>)

Aug 9 '06 #10
Kevin Handy <kt*@srv.netwrites:
3. If you know exactly where the dashes are, strncpy would be
much faster.
There is occasionally a good reason to use strncpy(). However:

* Using strncpy() into a large buffer can be very inefficient.
strncpy() always writes to every byte in the destination
buffer, which can waste a lot of time if the destination
buffer is much longer than the source string.

* If the source string is longer than the size of the
destination buffer, then strncpy() doesn't write a
terminating null. So a call to strncpy() must be followed
by explicitly writing a null terminator at the end of the
destination buffer in most cases.

And, in this case, if you know the length of the string and its
format, you might as well use memcpy(). Why hassle with the
weirdness of strncpy()?
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Aug 9 '06 #11
On Wed, 09 Aug 2006 14:29:41 -0600, Kevin Handy <kt*@srv.netwrote:
>3. If you know exactly where the dashes are, strncpy would be
much faster.

4, If you don't know where they are, loop through the array,
copying anything that ain't a dash.
Implementation dependent, of course, but I would bet on (4) being
faster than (3).

--
Al Balmer
Sun City, AZ
Aug 9 '06 #12
On Wed, 09 Aug 2006 19:52:03 GMT, Keith Thompson <ks***@mib.org>
wrote:
>"Carson" <c3***@yahoo.comwrites:
>Is it possible to remove the dashes in a social security number using
sprintf? If so what would the syntax look like? If sprintf won't do
this is there a C function that will?

I assume the SSN is stored as a string. You can copy the string to
another string, copying only the characters other than '-'. As you're
doing this, you'll need to indices, one into the source string and
another into the target string. This is likely to be easier, and
probably faster, than any method using sprintf() and/or functions from
<string.h>.

Since you're shortening the string, you could even do this in place,
but only if the original string is writable *and* you don't mind
clobbering it.
Good to know that at least one person recognizes that a spoon is
sometimes better than a bulldozer.

--
Al Balmer
Sun City, AZ
Aug 9 '06 #13


Carson wrote On 08/09/06 16:47,:
[...]
I do know exactly where the dashes are. The database column is
char(11) and the dashes are in positions 4 and 7. What is the syntax
used with strncpy, 'man strncpy' didn't give much info.

strncpy(variable, $table.socsecno, <what do I put here>)
("$table"? What's "$table"?)

If you're sure of the format, use memcpy():

char dashed[] = "123-45-6789";
char digits[3+2+4+1];
memcpy (digits, dashed, 3);
memcpy (digits+3, dashed+4, 2);
memcpy (digits+5, dashed+7, 4+1);

To do it "in place," use memmove():

char ssn[] = "123-45-6789";
memmove (ssn+3, ssn+4, 2);
memmove (ssn+5, ssn+7, 4+1);

strncpy() is not the hammer I would choose for driving
this particular screw. (IMHO, strncpy() is almost always
the wrong tool, no matter what the job -- but that's a rant
for a different thread.)

--
Er*********@sun.com

Aug 9 '06 #14


Al Balmer wrote On 08/09/06 17:34,:
On Wed, 09 Aug 2006 14:29:41 -0600, Kevin Handy <kt*@srv.netwrote:

>>3. If you know exactly where the dashes are, strncpy would be
much faster.

4, If you don't know where they are, loop through the array,
copying anything that ain't a dash.


Implementation dependent, of course, but I would bet on (4) being
faster than (3).
Implementation dependent, of course, but I would be on
any speed difference being completely negligible.

Wasn't there somebody a week or so ago who wanted to
optimize abort()?

--
Er*********@sun.com

Aug 9 '06 #15
On Wed, 09 Aug 2006 17:57:32 -0400, Eric Sosman <Er*********@sun.com>
wrote:
>

Al Balmer wrote On 08/09/06 17:34,:
>On Wed, 09 Aug 2006 14:29:41 -0600, Kevin Handy <kt*@srv.netwrote:

>>>3. If you know exactly where the dashes are, strncpy would be
much faster.

4, If you don't know where they are, loop through the array,
copying anything that ain't a dash.


Implementation dependent, of course, but I would bet on (4) being
faster than (3).

Implementation dependent, of course, but I would be on
any speed difference being completely negligible.
If you do it only once, certainly. If you do it a million times, it
depends on how aggressive the optimization is. If all the strncpy
calls were inlined it could be very close.

Actually, I would use (4) just because it's clearer to me and fits the
problem description - "remove dashes from the string." It's clearer to
me than three calls to strncpy, or even three calls to memcpy. What's
more, it's easily modifiable to "copy all numbers from the string", or
"copy the first nine digits you find". Easy to read, easy to
accommodate changing requirements.

If I were maintaining code that used strncpy in this application, it
would cost me extra time wondering why on earth the original
programmer did it that way ;-)
>
Wasn't there somebody a week or so ago who wanted to
optimize abort()?
Not that I saw. Do you see that as analogous to processing what may be
many millions of data entries? If the OP were only going to do it
once, he'd probably be using pencil and paper, not a computer.

--
Al Balmer
Sun City, AZ
Aug 10 '06 #16

J. J. Farrell wrote:
Carson wrote:
Is it possible to remove the dashes in a social security number using
sprintf?

Something along the lines of the following untested snippets would do
the trick:

char result[10];
char *ssn = "123-45-6789";

sprintf(result, "%.3s%.2s%.4s", ssn, ssn+4, ssn+7);
This did the job for me, thanks

Aug 10 '06 #17
Al Balmer wrote:
On Wed, 09 Aug 2006 17:57:32 -0400, Eric Sosman <Er*********@sun.com>
wrote:
>>
Al Balmer wrote On 08/09/06 17:34,:
>>>
Implementation dependent, of course, but I would bet on (4) being
faster than (3).

Implementation dependent, of course, but I would be on
any speed difference being completely negligible.

If you do it only once, certainly. If you do it a million times, it
depends on how aggressive the optimization is. If all the strncpy
calls were inlined it could be very close.
If you do it a million times, and you save a microsecond
apiece -- that's two or three thousand instructions, a pretty
substantial savings -- you will save a grand total of

O N E

S E C O N D

!!!!!

Whatever will you do with all that extra time? You'd better
not lallygag about deciding how to spend it, though: one of these
year's they'll declare another leap second, and bang! everybody
will have an extra second, and the bottom will drop out of the
market.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 10 '06 #18
Carson wrote:
>
J. J. Farrell wrote:
>Carson wrote:
Is it possible to remove the dashes in a social security number using
sprintf?

Something along the lines of the following untested snippets would do
the trick:

char result[10];
char *ssn = "123-45-6789";

sprintf(result, "%.3s%.2s%.4s", ssn, ssn+4, ssn+7);

This did the job for me, thanks
Tell me your not a govt programmer right? Please?!?
Eric

Aug 10 '06 #19
On Wed, 09 Aug 2006 21:51:35 -0400, Eric Sosman
<es*****@acm-dot-org.invalidwrote:
>Al Balmer wrote:
>On Wed, 09 Aug 2006 17:57:32 -0400, Eric Sosman <Er*********@sun.com>
wrote:
>>>
Al Balmer wrote On 08/09/06 17:34,:

Implementation dependent, of course, but I would bet on (4) being
faster than (3).

Implementation dependent, of course, but I would be on
any speed difference being completely negligible.

If you do it only once, certainly. If you do it a million times, it
depends on how aggressive the optimization is. If all the strncpy
calls were inlined it could be very close.

If you do it a million times, and you save a microsecond
apiece -- that's two or three thousand instructions, a pretty
substantial savings -- you will save a grand total of

O N E

S E C O N D

!!!!!

Whatever will you do with all that extra time? You'd better
not lallygag about deciding how to spend it, though: one of these
year's they'll declare another leap second, and bang! everybody
will have an extra second, and the bottom will drop out of the
market.
Don't start getting silly. Using your own numbers, if you need to
process a million records a second, you have no time at all left. You
aren't seriously advocating that there's no reason to write efficient
code using efficient algorithms, are you?

In the present case, it's even worse. Why write many lines of more
complex code which locks you into a specific representation, when a
more general solution can be implemented in a few lines, even if the
compiler magically turns it into identical binary (which it won't)?

--
Al Balmer
Sun City, AZ
Aug 10 '06 #20

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

Similar topics

163
by: Shiperton Henethe | last post by:
Hi Know any good utilities to help me strip out the tags that Microsoft Excel 2002 leaved behind when you try and export an HTML format file? This is driving me NUTS. And really makes me...
3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
1
by: Sue | last post by:
Hello I'm new to this Regular Expression and need some help. I want to restrict what the user types in a text box. The User can type only A-Z, a-z, 0-9, spaces, comma, dash, period, single...
1
by: mats.broberg | last post by:
Dear all, I'm having a scientific text translated into the majority of western and eastern European languages* and have provided a small leaflet with typesetting rules for the translators. Some...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
6
by: Chad | last post by:
I would like to remove the decimal but still show the cents using sprintf. char empheader; double totalwage = 123.45; sprintf(empheader, "%.2f", totalwage);
0
by: ClassicNancy | last post by:
If I want to remove the drop down for the year will that make the birthday not show on the calendar? If it looks like it is ok to remove it what can be removed?? What should it look like? ...
0
by: =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post by:
"C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 149, in Here it complains that it deals with the character U+2013, which is "EN DASH"; it complains that the encoding called "latin-1"...
61
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.