初學者問題
时间:2008-05-09 09:22:59
来源:论坛整理 作者: 编辑:chinaitzhe
請問要将一串字符中的数字去掉,生成一个新的字符串并输出?應該用什麽語句呢??
例如要將"abc123"中的数字去掉,生成一个新的字符串并输出...
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include<cstddef> #include<string> #include<iostream> using namespace std; int main() { cout<<"输入吧(分给我吧!-_-):"<<endl; string s,sum; cin>>s; for(size_t len=0;len!=s.size(); len) { if(s[len]>='0' && s[len] <='9') continue; sum =s[len]; } cout<<sum<<endl; return 0; }
网友回复:贴一个c语言的,呵呵...
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> int main() { char str[]="a1b3c34d35ef"; char tmp[20]; int i,k; for(i = 0,k = 0; str[i] != '\0'; i) { if(str[i] >= '0' && str[i] <= '9') continue; tmp[k ] = str[i]; } tmp[k] = '\0'; printf("%s\n",tmp); return 0; }
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ bool isNum(char c){ return c >= '0' && c <= '9'; } int main() { char p[] = "sfo232oj2j2"; cout << p << endl; char *c = remove_if(p, p strlen(p), isNum); *c = '\0'; cout << p << endl; } void fun(char *const s){ cout << s << endl; }
网友回复:没有下面的fun函数
网友回复:MARK
网友回复:
size_t是什么?在LINUX下见过,就是不知道是什么
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include<iostream> #include<string> #include<cctype> using namespace std; int main() { string st1,st2; cout<<"请输入带数字的字符串:"<<endl; cin>>st1; for (string::size_type i=0; i<st1.size(); i) //用下标遍历字符串中的字符 { if (isdigit(st1[i])) i ; //假如是数字,下标增加1,即移动到该数字后面的字符,继续判定。 else st2.push_back(st1[i]); //假如不是数字,将该字符置入字符串st2。 } cout<<"不含数字的字符串是 "<<st2<<endl; system("pause"); }
网友回复:To:6L
bitset类型的配套类型
是无符号型的...
s.size();返回的是s中的二进制位个数,它的返回类型是size_t
你去看看string和vector和bitset
就知道了,他们每个都有它自己的配套类型的
网友回复:mark
网友回复:
string 不是也有string::size_type吗?
size_t和size_type有什么区别吗???
网友回复:真有意思
网友回复:size_t定义于头文件stddef.h,它是机器特定的无符号整数类型,是typedef出来的
string::size_type表示的是string类中的size_type类型,这个类型是一个无符号数整型,用来描述字符串变量中的容量和大小
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ //上面已经写的可以了。为了全面我用 STL来写吧! #include "stdafx.h" #include<iostream> #include <string> #include <algorithm> void Print(std::string::const_iterator &beg,std::string::const_iterator &end) { while(beg!=end) { std::cout<<*beg; beg; } } int main() { std::string str1("0123456789"); std::string str; std::cout<<"Iuput a string:"; std::cin>>str; std::string::iterator beg=str.begin(); while(beg!=str.end()) { beg=find_first_of(str.begin(),str.end(),str1.begin(),str1.end()); if(beg!=str.end()) str.erase(beg); } Print(str.begin(),str.end()); return 1; }
网友回复:大家的思路都一样呀!
关键字:初學者問題,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面