使用sprintf删除ssn中的破折号 [英] remove dash in ssn with sprintf

查看:82
本文介绍了使用sprintf删除ssn中的破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用

sprintf删除社会安全号码中的短划线?如果是这样,语法会是什么样子?如果sprintf不做

这会有一个C函数吗?

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?

推荐答案

Carson写道:
Carson wrote:

是否可以使用

sprintf删除社会安全号码中的短划线?如果是这样,语法会是什么样子?如果sprintf不做

这是否会有一个C函数?
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?



我的这种东西的金锤是正则表达式。但是如果数据是保证一致的话,我可能会用指针和

strchr()来做。我不知道你是怎么用只是这样做的。 sprintf的。如果你有
有3个数字部分而且你想把破折号*放在*中,那么使用sprintf就可以很容易了。

也许sscanf或strtok的工作。

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.


Carson< c3 *** @ yahoo.comwrote:
Carson <c3***@yahoo.comwrote:

是否可以使用

sprintf删除社会安全号码中的短划线?如果是这样,语法会是什么样子?如果sprintf不做

这是否会有C函数?
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()可以解决这个问题:


#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);

返回0;

}


(当然,假设字符串在一个已知的格式。错误

检查等等。)


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。

*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.


" Carson" < c3 *** @ yahoo.comwrites:
"Carson" <c3***@yahoo.comwrites:

是否可以使用

sprintf删除社会安全号码中的短划线?如果是这样,语法会是什么样子?如果sprintf不做

这是否会有C函数?
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?



我假设SSN存储为字符串。您可以将字符串复制到

另一个字符串,仅复制除'' - ''以外的字符。当你这样做时,你需要索引,一个到源字符串中,另一个是
另一个到目标字符串中。这可能比使用sprintf()和/或来自

< string.h>的函数的任何方法更容易,并且可能更快。




因为你正在缩短字符串,你甚至可以这样做,

但只有当原始字符串可写*和*你不介意

破坏它。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http ://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

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.


这篇关于使用sprintf删除ssn中的破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆