字符串算法
字符串逆转
void reverse(char* str,int beg,int end) { int times = (end- beg +1)/2; while(times > 0) { char tmp = str[end]; str[end--] = str[beg]; str[beg++] = tmp; times--; } }
字符串包含
bool is_substr(char* str1,char* str2) { int i = 0,j = 0; while(str1[i] != '\0') { while(str2[j] != '\0' && str1[i] == str2[j] ) { i++; j++; } if(str2[j] == '\0') return true; else j = 0; i++; } return false; }
strncpy
char* _strncpy(char* dest, const char* src, int len) { assert('\0') error assert(dest!=NULL && src!=NULL); char* temp=dest; int i=0; while(i++ < len && (*temp++ = *src++)!='\0') {} if(*(--temp)!='\0') *temp='\0'; return dest; }
strcpy
char* _strcpy(char* dest,const char* src) { assert(dest!=NULL && src!=NULL); char* temp=dest; while((*temp++ = *src++ )!='\0') {} return dest; }
本博客内容均来自网络,如有雷同,是我抄袭!