怎么除去字符串里的数字

woshi420 2009-04-18 04:56:21
刚学编程 很菜 别见笑
老师给了个题目 除去字符串里的数字 我写了个程序 可是不行
大家帮我改下
#include<iostream.h>
char * ab(char *);
int main()
{
char a[10];
cin>>a;
char *e;
e=ab(a);
cout<<e<<endl;
return 0;
}
char* ab(char *a)
{
char d[10];
int n=0,m=0;
while (1)
{
if(a[n]<='9'&&a[n]>='0')
n++;
d[m]=a[n];
if(a[n]=='\0')
{
d[m]=a[n];
break;
}
n++,m++;
}
return d;
}
...全文
132 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
adventurelw 2009-04-18
  • 打赏
  • 举报
回复
用string标准库最简单,反正C字符串可以直接转成string。
#include <iostream>
#include <cctype>
#include <string>
int main()
{
string str;
std::cout << "Enter a string:\n";
getline(std::cin, str);
std::string::iterator iter = str.begin();
while(iter != str.end())
{
if(std::isdigit(*iter))
iter = str.erase(iter);
else
++iter;
}
std::cout << str << std::endl;
return 0;
}
hangyu628 2009-04-18
  • 打赏
  • 举报
回复
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
const char *cp = "Hello world 12433, and you are good.";
while (*cp)
{
if (!isdigit(*cp))
cout<<*cp;
++cp;
}

return 0;
}

也可以用如上isdigit这个函数.
liubuweiright 2009-04-18
  • 打赏
  • 举报
回复
学习...
qqwx_1986 2009-04-18
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
char * ab(char *);
int main()
{
char a[100];
cin>>a;
char *e=ab(a);
cout <<e <<endl;
delete e;
system("pause");
return 0;
}
char* ab(char *a)
{
char *d=new char[strlen(a)+1];
int n=0,m=0;
while (1)
{
if(a[n] <='9'&&a[n]>='0')
{
n++;
continue;
}
d[m]=a[n];
if(a[n]=='\0')
{
d[m]=a[n];
break;
}
n++,m++;
}
return d;
}

65,211

社区成员

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

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