写一个电话号码中特殊字符的过滤的C++算法

gccdy 2007-12-07 05:06:56
帮忙写一个电话号码中特殊字符的过滤的C++算法
...全文
122 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
vrace 2007-12-07
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>

int GetNum(char *pszDest, const char *pszSource)
{
int c;

c = 0;
while(*pszSource)
{
if(*pszSource >= '0' && *pszSource <= '9')
{
if(pszDest) pszDest[c] = *pszSource;
c++;
}
pszSource++;
}

return c;
}

int main(void)
{
char *str = "+86 123-4567890#";
char *pDest;
int c;

c = GetNum(NULL, str);
if(!c) return 1;

pDest = (char*)malloc(sizeof(char) * (c + 1));
GetNum(pDest, str);

printf("%s\n", pDest);
free(pDest);

return 0;
}
gccdy 2007-12-07
  • 打赏
  • 举报
回复
呵呵,谢谢了!!!
xalangying 2007-12-07
  • 打赏
  • 举报
回复
我在三楼写错了,改正
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
using namespace std;

int main()
{
string snum="+86 20-1234567#";
snum.erase(remove_if(snum.begin(), snum.end(), not1( ptr_fun(isdigit))), snum.end());
cout<<snum<<endl;
return 0;
}
qiuqiu173 2007-12-07
  • 打赏
  • 举报
回复
楼主胶泥一个最简单的方法:


int isdigit(int ch)//测试参数是否为数字 0 - 9

believefym 2007-12-07
  • 打赏
  • 举报
回复

char c[20]="+86 20-1234567#";

int j=0;
for(int i=0; i<strlen(c); ++i)
{
if(c[i]=='+'||c[i]=='-'||c[i]=='#')
continue;
c[j++]=c[i];
}

c[j]=0;

cout<<c<<endl;
believefym 2007-12-07
  • 打赏
  • 举报
回复

#include<string>
#include<algorithm>
using namespace std;

bool is(char c)
{
static const string str="+-# ";
return str.find(c)!=string::npos;
}
void main()
{
string snum="+86 20-1234567#";
erase(remove_if(snum.begin(), snum.end(), is), snum.end());
}
believefym 2007-12-07
  • 打赏
  • 举报
回复
char c[20]="+86 20-1234567#";

int j=0;
for(int i=0; i<strlen(c); ++i)
{
if(c[i]=='+'||c[i]=='-'||c[i]=='#')
continue;
c[j++]=c[i];
}

c[j]=0;

cout<<c<<endl;
xalangying 2007-12-07
  • 打赏
  • 举报
回复
#include<string>
#include<algorithm>
using namespace std;

bool is(char c)
{
static const string str="+-# ";
return str.find(c)!=string::npos;
}
void main()
{
string snum="+86 20-1234567#";
erase(remove_if(snum.begin(), snum.end(), is), snum.end());
}
gccdy 2007-12-07
  • 打赏
  • 举报
回复
比如: +86 20-1234567# 怎么样把+ - # (空格)过滤掉
believefym 2007-12-07
  • 打赏
  • 举报
回复
需求不清

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧