当前位置:首页 > 《C%2B%2B数组与指针习题》
数组与指针习题 13
mystring ms1(sp1),ms2(sp2),ms3(sp3),ms4(ms3),ms5=ms3,ms6; ms1.show(); ms2.show(); ms3.show(); ms4.show(); ms5.show(); ms6.show(); ms4=ms1+ms3; ms4.show(); ms1+=ms2; ms1.show();
if(ms1 else {ms1.show();cout<<\应排在\之后\ ms6=ms1; if(ms1==ms6) cout<<\串ms1与串ms6相同\} 5.12 将习题5.8中的字符串处理函数移植到mystring类中,其中strcat已重载为+运算符, 请将其它4个转为成员函数。对比成员函数与独立函数构造上有何不同? #include char* strcat(char* s,const char* ct){ while(*s) s++;//*S作为条件,等效*S!=0 while(*s++=*ct++); return s; } int strlen(const char* s){ int i=0; while(*s++) i++; return i; } char* reverse (char* s){ char temp,* temp1=s,* temp2=s; while(*temp2) temp2++; temp2--;//指针移回串尾 while(temp2-temp1>0){ //注意此处,从串两头的指针同时向中间移动,重合或交错时停止 temp=*temp1; *temp1=*temp2; *temp2=temp; temp1++; temp2--; 数组与指针习题 14 } return s; } char* strchr( const char*cs,char c){ while(*cs!=c&&*cs) cs++; if(*cs==0) cs=NULL; //未找到返回NALL return (char*)cs; } char *strstr (const char *cs1,const char *cs2){ char *temp; char *temp1=(char*)cs2; while(*cs1){//只要主串还有字符未查,则继续 while(*cs1!=*cs2&&*cs1) cs1++;//找到主串含有子串的第一个字符,或主串查完停止 temp=(char*)cs1; temp1=(char*)cs2; if(*cs1){//核对子串其他字符 while(*cs1++==*temp1++||*temp1); if(*temp1==0) return temp;//找到子串返回 } } return NULL; //未找到返回NAL } void main(){ char a[40]=\李明\ char b[20]=\是东南大学学生\ char c[40]=\ char *cp; cout< cout<<\字符串连接后:\ cout< cout<<\字符串长度为:\ cout< if(cp==NULL) cout<<\未找到\ else cout< if(cp==NULL) cout<<\未找到\ else cout< if(cp!=NULL) cout< 数组与指针习题 15 } else cout<<\未找到\cp=strstr(a,\西北\ if(cp==NULL) cout<<\未找到\else cout<
共分享92篇相关文档