<1> 方法1:循環遍歷 [cpp] #include<iostream> #include<string> using namespace std; bool IsSubstring(char* src,char* des) { int len = strlen(src); for(int i = 0;i < len;i++) { if(strstr(src,des) != NULL) return true; char tempchar = src[0]; for(int j = 0;j < len -1;j++) src[j] = src[j+1]; src[len - 1] = tempchar; } return false; } void main() { char src[20] = "abc" , des[20] = "bcd"; if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; strcpy(src, "abcdef"),strcpy(des,"bc"); if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; } /* NO OK 總結: 1,注意在IsSubstring裡面,即使是char src[],也依然退化為指針,你用字符串常量就會出錯 2,即使為char*,依然可以用strlen函數,因為它判斷的是'\0',退化為指針後不能用sizeof 3,開始使用的是char[]全局變量的時候,注意分配內存的大小已經確定,以後在strcpy的時候注意別數組越界 */ #include<iostream> #include<string> using namespace std; bool IsSubstring(char* src,char* des) { int len = strlen(src); for(int i = 0;i < len;i++) { if(strstr(src,des) != NULL) return true; char tempchar = src[0]; for(int j = 0;j < len -1;j++) src[j] = src[j+1]; src[len - 1] = tempchar; } return false; } void main() { char src[20] = "abc" , des[20] = "bcd"; if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; strcpy(src, "abcdef"),strcpy(des,"bc"); if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; } /* NO OK 總結: 1,注意在IsSubstring裡面,即使是char src[],也依然退化為指針,你用字符串常量就會出錯 2,即使為char*,依然可以用strlen函數,因為它判斷的是'\0',退化為指針後不能用sizeof 3,開始使用的是char[]全局變量的時候,注意分配內存的大小已經確定,以後在strcpy的時候注意別數組越界 */方法2:將字符串s1換成s1s1,看s2是否為s1s1子串 [cpp] #include<iostream> #include<string> using namespace std; bool IsSubstring(char* src,char* des) { char* srcTemp = new char[strlen(src)+1]; strcpy(srcTemp,src); srcTemp[strlen(src)] = '\0'; src = strcat(src,srcTemp); if(strstr(src,des) != NULL) { delete[] srcTemp; return true; } else { delete[] srcTemp; return false; } } void main() { char src[20] = "abcdef" , des[20] = "bcdg"; if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; strcpy(src, "abcdef"),strcpy(des,"bc"); if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; } /* NO OK 總結: 1,char *strstr( const char *string, const char *strCharSet ); char str[] = "lazy"; char string[] = "The quick brown dog jumps over the lazy fox"; char* pdest = strstr( string, str );則pdest = lazy fox; 2,關於strcat,MSDN上有一段話No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.也就是說源串和目的串不能重疊,否則出錯。所以strcat(s,s)是未定義的 */ #include<iostream> #include<string> using namespace std; bool IsSubstring(char* src,char* des) { char* srcTemp = new char[strlen(src)+1]; strcpy(srcTemp,src); srcTemp[strlen(src)] = '\0'; src = strcat(src,srcTemp); if(strstr(src,des) != NULL) { delete[] srcTemp; return true; } else { delete[] srcTemp; return false; } } void main() { char src[20] = "abcdef" , des[20] = "bcdg"; if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; strcpy(src, "abcdef"),strcpy(des,"bc"); if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; } /* NO OK 總結: 1,char *strstr( const char *string, const char *strCharSet ); char str[] = "lazy"; char string[] = "The quick brown dog jumps over the lazy fox"; char* pdest = strstr( string, str );則pdest = lazy fox; 2,關於strcat,MSDN上有一段話No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.也就是說源串和目的串不能重疊,否則出錯。所以strcat(s,s)是未定義的 */<2>自定義strcat之MyStrcat,解密src與des內存不能重疊原因。 [cpp] #include<iostream> #include<string> #include<cassert> using namespace std; //////////////////////////////////// char* MyStrcat(char* src,char* des) { assert(src!=NULL && des!=NULL); int srcEnd = 0; while(1) { if(src[srcEnd] != '\0') srcEnd++; else break; } char* pdes = des; while(1) { if(*pdes != '\0') { src[srcEnd] = *pdes; srcEnd++; pdes++; } else { src[srcEnd] = '\0'; break; } } return src; } //////////////////////////////////// bool IsSubstring(char* src,char* des) { char* srcTemp = new char[strlen(src)+1]; strcpy(srcTemp,src); srcTemp[strlen(src)] = '\0'; src = MyStrcat(src,srcTemp); if(strstr(src,des) != NULL) { delete[] srcTemp; return true; } else { delete[] srcTemp; return false; } } void main() { char src[20] = "abcdef" , des[20] = "bcdg"; if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; strcpy(src, "abcdef"),strcpy(des,"bc"); if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; } /* NO OK 總結:可以看出,當src和des內存一樣時,因為src一直在增長著,指向des的指針pdes將不知道到哪裡結尾,導致出錯。 */ #include<iostream> #include<string> #include<cassert> using namespace std; //////////////////////////////////// char* MyStrcat(char* src,char* des) { assert(src!=NULL && des!=NULL); int srcEnd = 0; while(1) { if(src[srcEnd] != '\0') srcEnd++; else break; } char* pdes = des; while(1) { if(*pdes != '\0') { src[srcEnd] = *pdes; srcEnd++; pdes++; } else { src[srcEnd] = '\0'; break; } } return src; } //////////////////////////////////// bool IsSubstring(char* src,char* des) { char* srcTemp = new char[strlen(src)+1]; strcpy(srcTemp,src); srcTemp[strlen(src)] = '\0'; src = MyStrcat(src,srcTemp); if(strstr(src,des) != NULL) { delete[] srcTemp; return true; } else { delete[] srcTemp; return false; } } void main() { char src[20] = "abcdef" , des[20] = "bcdg"; if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; strcpy(src, "abcdef"),strcpy(des,"bc"); if(IsSubstring(src,des)) cout<<"OK"<<endl; else cout<<"NO"<<endl; } /* NO OK 總結:可以看出,當src和des內存一樣時,因為src一直在增長著,指向des的指針pdes將不知道到哪裡結尾,導致出錯。 */