随笔分类 -  String

摘要:1.strcpystrcpy是拷贝字符串,以\0为标志结束strcpy的原型为char *strcpy_(char *strDest, const char *strScr){ assert((strDest != NULL) && (strScr != NULL)); char *address=strDest; while((*strDest++ = * strScr++) != '\0') NULL ; *strDest = '\0'; //当strScr字符串长度小于原strDest字符串长度时,如果没有改语句,就会出错了。 return 阅读全文
posted @ 2012-11-14 17:09 byfei 阅读(224) 评论(0) 推荐(0) 编辑
摘要:class String{public:String(const char *str ); // 通用构造函数String(const String &another); // 拷贝构造函数~ String(); // 析构函数String & operater =(const String &rhs); // 赋值函数private:char *m_data; // 用于保存字符串};尝试写出类的成员函数实现。答案:String::String(const char *str){if ( str == NULL ) //strlen在参数为NULL时会抛异常才会有这步 阅读全文
posted @ 2012-06-27 19:27 byfei 阅读(86) 评论(0) 推荐(0) 编辑
摘要:void str(char * a){char *toks = " ";char * tok = strtok(a, toks);while (tok){if (tok == a)strcpy(a, tok);elsestrcat(a, tok);tok = strtok(NULL, toks);}}int main(){string b = "this is a dog";str(const_cast<char*>(b.c_str()));cout<<b;return 0;}#include <iostream> # 阅读全文
posted @ 2012-05-15 15:29 byfei 阅读(477) 评论(0) 推荐(0) 编辑
摘要:#include <iostream>#include <string.h>using namespace std;int main(){unsigned char input[50];cin >> input;int flag = 0;for (int i = 0; i < 50; i++){if (input[i] > 0xa0 && input[i] != 0){if (flag == 1){cout << "chinese character" << endl;flag = 0; 阅读全文
posted @ 2012-05-12 09:47 byfei 阅读(193) 评论(0) 推荐(0) 编辑
摘要:#include <iostream>#include <string.h>using namespace std;int main(){string result;string s ( "AAAAAAAA" );char ch = 'C';result = s.replace ( 1 , 3 , 4 , ch ); // s= "ACCCCAAAAcout<<s<<endl;return 0;} 阅读全文
posted @ 2012-05-11 19:17 byfei 阅读(159) 评论(0) 推荐(0) 编辑
摘要:#includeusing namespace std;#includeint main( ){ char test01 [100]="你被禁言";char strTime [50];sprintf(strTime,"%d",100);strcat(test01,strTime);strcat(test01,"秒");cout<<test01<<endl;system("pause");return 0;} 阅读全文
posted @ 2011-11-11 09:43 byfei 阅读(125) 评论(0) 推荐(0) 编辑
摘要:例子: string s, ss; . s = "The rain in Spain falls mainly in the plain."; ss = s.substr(12, 5); //Spain s.substr(12) //Spain falls mainly in the plain. 阅读全文
posted @ 2011-10-18 10:44 byfei 阅读(193) 评论(0) 推荐(0) 编辑
摘要:#include <string.h>#include <iostream>using namespace std;int main(){char d[256]="hello";//char *s=" word";char s[256]=" word";strcat(d,s);cout<<d;getchar();return 0;} 阅读全文
posted @ 2011-09-21 11:48 byfei 阅读(261) 评论(0) 推荐(0) 编辑