c++读取文件中的字母并输出 用isalpha

baidu_37685167 2018-01-06 02:59:45

可以按照这个isdigit 读取文件中数字的方法 来用isalpha来读取文件中字母的方法吗

我的isalpha写不来

我用上面的代码运行出来的是这样的



old.txt中是:hello123khsdue7dhsi8%$#jd983dds

所以想让大神用类似的方法 用isalpha写一个能使运行结果是:hello khsdue dhsi jd dds
的c++代码

谢谢了!
...全文
320 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_37685167 2018-01-07
  • 打赏
  • 举报
回复
#include "stdafx.h" #include<fstream> #include<iostream> #include<string> #include<algorithm> #include<cctype> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { std::ifstream istr("D:\old.txt"); if(istr.is_open()) { std::string tmp; while(getline(istr, tmp)) { std::string::const_iterator p, q = tmp.begin(), end = tmp.end(); while((p = find_if(q, end, isdigit)) != end) {//查看该行是否还有数字 q = find_if_not(p, end, isdigit);//查找最后一个不是数字的位置 std::cout<<stoul(tmp.substr(distance(tmp.cbegin(), p), distance(p, q)))<<" ";//将p~q的字串转化为数字 } } } return 0; }
baidu_37685167 2018-01-07
  • 打赏
  • 举报
回复
baidu_37685167 2018-01-06
  • 打赏
  • 举报
回复
可是我是从文件中打开的这串字符串 不是直接在代码里写的
百灵工作室 2018-01-06
  • 打赏
  • 举报
回复
void test() { std::string a = "hello123khsdue7dhsi8%$#jd983dds"; int len = a.size(); for (int i = 0; i < len; ++i) { if (isalpha(a[i])) printf("%c", a[i]); else printf(" "); } printf("\n"); }

65,194

社区成员

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

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