468,504 Members | 2,001 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

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 3413
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by huey_jiang | last post: by
1 post views Thread by mats.broberg | last post: by
12 posts views Thread by Henryk | last post: by
6 posts views Thread by Chad | last post: by
reply views Thread by =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post: by
61 posts views Thread by arnuld | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.