C語言實現常見的字串處理函式
阿新 • • 發佈:2019-01-02
#if 0 ///strcpy字串賦值函式實現 ///法1 /*char my_strcpy(char *dest, const char *src) { assert(dest!=NULL && src != NULL); while((*dest++ = *src++) != '\0'); }*/ ///法2 void my_strcpy(char* Dest, const char *Src) { assert((Dest!= NULL)&&(Src!=NULL)); while((*Dest = *Src)!='\0'){ Dest++; Src++; } } int main() { int a=3; char dest[]="abc"; ///char *dest="abc"; ///error 這樣傳過去的是個地址,不能把字串賦值再指標中 且字元常量存放在常量區 //char src[]="def"; char *src="def"; ///can use my_strcpy(dest,src); printf("%s\n",dest); return 0; } #endif #if 0 ///strcat 字串追加函式實現 char *my_strcat(char *dest, const char *stc) { char *temp; assert(dest!=NULL && stc != NULL); while(*dest) dest++; while((*dest++ = *stc++) != '\0'); return temp; } int main() { int a=3; //char dest[20]="abc"; char dest[]="abc"; char *src="def"; ///can use my_strcat(dest,src); printf("%s\n",dest); return 0; } #endif // 1 #if 0 ///strstr字串中子串查詢函式實現 /*char my_strstr(char *str, char c) ///no use { for(; *str != c; str++) if(*str == '\0') return NULL; else{ printf("str=%s\n",str); return str; } }*/ const char *my_strstr(char *str, char *sub_str) { int i; //assert(NULL != str && NULL != sub_str); if(str==NULL||NULL==sub_str) { return str; } const char *bp=str; const char *sp=sub_str; ///法1 更清晰 for( i= 0; bp[i] != '\0'; i++) { int tem = i; //tem保留主串中的起始判斷下標位置 int j = 0; while(bp[i++] == sp[j++]) { if(sp[j] == '\0') { return &bp[tem]; } } i = tem; } ///法2 can use //while(*str) //{ // bp=str; //sp=sub_str; //do{ //if(!*sp) //return str; //}while(*bp++==*sp++); //str+=1; //} ///查看出現次數 //int sum=0,ln=strlen(sub_str); //while(str=strstr(str,sub_str)){ // sum++; //str+=ln; //} //return sum; return NULL; } int main() { const char dest[]="abcefg"; char *src="ce"; ///can use //char *dest="abcefg"; //char src='c'; ///no use char *r=my_strstr(dest,src); printf("%s\n",r); printf("%d\n",r-dest+1); ///返回索引位置 return 0; } #endif // 1 #if 0 ///strlen字串長度統計函式實現 int my_strlen(const char *str) { assert(NULL != str); char *temp = str; for(; *temp != '\0'; temp++); return (int)(temp - str); } int main() { const char dest[]="abcefg"; int num=my_strlen(dest); printf("%d\n",num); return 0; } #endif // 1 #if 0 ///memcpy字串copy函式實現 char *my_memcpy(char * dest, const char *src, size_t count) { ///char *temp_dest = (char *)dest; ///char *temp_src = (char *)src; //assert(dest != NULL && src != NULL); ///assert(dest>=src+count||src>dest+count);//考慮記憶體重疊的情況 ///while(count--) ///*temp_dest++ = *temp_src++; int wordnum = count/4;//計算有多少個32位,按4位元組拷貝 int slice = count%4;//剩餘的按位元組拷貝 printf("wordnum=%d,slice=%d\n",wordnum,slice); int * pintsrc = (int *)src; int * pintdst = (int *)dest; while(wordnum--)*pintdst++ = *pintsrc++; char *pTempDst = (char *)pintdst; char *pTempSrc = (char *)pintsrc; printf("wordnum=%d, slice=%d\n",wordnum,slice); while(slice--) { *pTempDst++ = *pTempSrc++; } return dest; } int main() { char dest[20]="abcefg"; const char src[]="opqrstxyz"; printf("%d\n",sizeof(src)); char *r=my_memcpy(dest, src, sizeof(src)-1); printf("%s\n",r); return 0; } #endif // 1 #if 0 ///strcmp字串比較函式實現 int my_strcmp(const char *str1, const char *str2) { int ret = 0; assert(str1 != NULL && str2 != NULL); ///法1:減法 //while(!(ret = *(unsigned char*)str1 - *(unsigned char*)str2)&&*str1&&*str2) //{ //str1++; //str2++; //} ///法2:==法 /* while(*str1 == *str2 && *str1 != '\0'&&*str2) { str1++; str2++; } ret= *str1 - *str2;*/ if(ret < 0) ret = -1; else if(ret > 0) ret = 1; return ret; } int main() { const char dest[]="abcefg"; const char src[]="abqrstxyz"; int result=my_strcmp(dest, src); printf("%d\n",result); return 0; } #endif // 1 #if 0 //輸出具有指定位置匹配的字串的句子 #define MAXSIZE 100 int getline(char line[],int max); int strindex(char src[],char searchfor[]); char pattern[]="ould"; int main() { char line[MAXSIZE]; int found=0; while(getline(line,MAXSIZE)) if(strindex(line,pattern)>=0){ printf("%s ",line); int num=strindex(line,pattern); printf("%d \n",num); found++; } return found; } int getline(char s[],int lim) { int c,i; i=0; while(--lim>0 && (c=getchar())!=EOF && c!='\n') s[i++]=c; if(c=='\n') s[i++]=c; s[i]='\0'; return i; } int strindex(char s[],char t[]) { int i,j,k; ///左側位置 for(i=0;s[i]!='\0';i++){ for(j=i,k=0;s[j]==t[k];j++,k++) ; if(j>0 && t[k]=='\0') return i; } /*char *bp=s; int sum=0,ln=strlen(t); while(bp=strstr(bp,t)){ sum++; bp +=ln; } printf("num=%d\n",sum); return (bp-s+1);*/ ///右側位置 /*for (i = strlen(s) - 1; i >= 0; i--) { for (j = i, k = strlen(t) - 1; k >= 0 && s[j] == t[k]; j--, k--) ; if (k < 0) return j+1; }*/ return -1; } #endif // 1