1.strcat:
(1)函式功能:實現兩個字串的連線
(2)思想:首先遍歷目標字串,找到『\0』的位址,然後將資源字串通過指標一次一次的拼接在目標字串後面,直到指標走到資源字串的『\0』;
(3)**:
char *mystrcat(char *strdestination, const char *strsource)
while (*strsource != '\0')
return p;
}
2.strchr:
(1)函式功能:在字串中尋找某字元,如果這個字串有這個字元返回首次出現這個字元的位址,否則返回null;
(2)思想:通過指標遍歷整個字串尋找指定字元,找到就返回它的位址,遍歷完整個字串,仍然沒有找到,則返回null;
(3)**:
char *mystrchr(const char *string, int c)
p++;
} return null;
}
3.strstr
(1)函式功能:在目標字串中尋找資源字串首次出現的位置,如果找到,返回其位址,沒有則返回null;
(2)思想:分別定義兩個指標,分別指向目標字串和資源字串,遍歷整個目標字串,比較目標字串和資源字串中的元素,相等兩個字串同時往下走,不同時,目標字串往下走,繼續與資源字串比較,直到遍歷完整個目標字串,返回null,或者找到資源字串,返回其位址
(3)**:
char *mystrstr(const char *string, const char *strcharset)
}else
}return null;
}
4.strcmp:
(1)函式功能:比較兩個字串大小。前者大返回1,後者大返回-1,否則返回0;
(2)思想:分別遍歷兩個字串,通過指標的移動對兩個字串相應的位置進行比較
(3)**:
int mystrcmp(const char *string1, const char *string2)
string1++;
string2++;
} if (*string1 > *string2)
}
1.strncpy
(1)函式功能:拷貝給定個字元從資源字串到目標字串。
(2)思想:通過指標遍歷資源字串將指標所對應位置上的字串賦給目標字串的相應位置
(3)**:
char *mystrncpy(char *strdest, const char *strsource, size_t count)
return p;
}
2.strncat
(1)函式功能:從資源字串中取出給定個字元連線在目標字串後面
(2)思想:通過指標遍歷到目標字串的』\0』的位置,將給定的字元一一新增在後面
(3)**:
char *mystrncat(char *strdest, const char *strsource, size_t count)
for (i = 0; i < count; i++)
*strdest = '\0';
return p;
}
3.strncmp:
(1)函式功能:比較兩個字串前n個字串的大小
(2)思想:通過給定的比較個數控制迴圈次數,迴圈內通過指標遍歷所需的字元進行比較
(3)**:
int mystrncmp(const char *string1, const char *string2, size_t count)
else
string1++;
string2++;
} if (*string1 > *string2)
else
}
1.memmove
(1)函式功能: 用於拷貝位元組,如果目標區域和源區域有重疊的話,memmove能夠保證源串在被覆蓋之前將重疊區域的位元組拷貝到目標區域中,但複製後源內容會被更改。但是當目標區域與源區域沒有重疊則和memcpy函式功能相同。
(2)思想:判斷指向目標字串的指標是否大於指向資源字串的指標,大於則進行正常的拷貝,否則,將兩個指標都加上給定位元組數減1在進行拷貝操作
(3)**:
void * mymemmove(void * dest, const void * src, size_t count)
} else
} return ret;
}
2.memcpyvoid *mymemcpy(void *dest, const void *src, size_t num)
return ret;
}
C語言 字串庫函式的模擬實現
1.實現strlen 字串長度 1.計數器方式 intmy strlen const char str return count 2.不建立臨時變數計數器 遞迴 intmy strlen const char str else 3.指標 指標的方式 intmy strlen const char s...
有關字串操作的的庫函式模擬實現
通常,有關字串操作的庫函式有strcpy,stract,strcmp,strstr,memcpy和memmove等。本文中將模擬實現這些庫函式。include include includechar my strcpy char crr,char drr crr drr 將 0 拷進去 return...
字串模擬實現
1.三種方式模擬實現strlen函式。方法1 用計數器模擬實現 define crt secure no warnings include include include include int mystrlen char str return ret int main printf 請輸入字串 n...