- 模拟实现strlen
- 模拟实现strcpy
- 模拟实现strncpy
- 模拟实现strcat
- 模拟实现strncat
- 模拟实现strcmp
- 模拟实现strncmp
- 模拟实现strstr
- 模拟实现strchr
函数原型:
size_t strlen(const char* str);
#define _CRT_SECURE_NO_WARNINGS 1 #include模拟实现strcpy#include unsigned my_strlen(const char* str) { unsigned count = 0; assert(str);//断言一下,保证指针的有效性,也可以写成assert(str!=NULL); while (*str != ' ') { count++; str++; } return count; } int main() { char str[30] = { 0 }; gets(str);//输入一行字符串 unsigned len = my_strlen(str); printf("%dn", len); return 0; }
函数原型:
char* strcpy(char* destination,const char* source);
#include模拟实现strncpy#include char* my_strcpy(char* dest, const char* sour) { char* tmp = dest; assert(dest && sour);//也可以写成assert(dest!=NULL && sour!=NULL); while (*sour != ' ') //也可以写成while(*dest++=*sour++){;} { *dest++= *sour++;//先解引用再++,也可写成*dest=*sour;dest++;sour++; } *dest = *sour; return tmp; } int main() { char str1[30] = "Life depends not on luck"; const char* str2 = "good"; char* ret = my_strcpy(str1, str2); printf("str1=%sn", str1);//good return 0; }
函数原型:
char* strncpy(char* destination,const char* source,size_t n);
#define _CRT_SECURE_NO_WARNINGS 1 #include#include char* my_strcpy(char* dest, const char* sour,unsigned n) { char* tmp = dest; assert(dest && sour); while (n) { *dest++ = *sour++; n--; } return tmp; } int main() { char str1[30] = "Life depends not on luck"; const char* str2 = "good"; unsigned n=0; scanf("%u", &n); char* ret = my_strncpy(str1, str2,n); printf("str1=%sn", str1); return 0; } 输入:3 则输出:str1=gooe depends not on luck 输入:4 则输出:str1=good depends not on luck 输入:5 则输出:str1=good 输入:8 则输出:str1=good
↑想知道为什么请看这
模拟实现strcat函数原型:
char* strcat(char* destination,const char* source);
#include模拟实现strncat#include int main() { char* my_strcat(char*, const char*); char str1[20] = "Life"; const char* str2 = "Good"; char* ret = my_strcat(str1, str2); printf("%sn", str1); return 0; } char* my_strcat(char* dest, const char* sour) { char* tmp = dest; assert(dest && sour); //找到目的字符串的' ' while (*dest)//这里不可以写成while(*dest++){;} 因为当*dest=' '时,条件判断虽然为假,但是dest还是会++,导致最后str1中的状态为Life Good { dest++; } while (*dest++ = *sour++)//追加 //注意:当*sour=' '时,即*dest=' ',条件为假,此时dest和sour还是会++ { ; } return tmp; }
函数原型:
char* strcat(char* destination,const char* source,size_t n);
#define _CRT_SECURE_NO_WARNINGS 1 #include模拟实现strcmp#include int main() { char* my_strncat(char*, const char*,unsigned); char str1[20] = "Life"; const char* str2 = "Good"; unsigned n = 0; scanf("%u",&n); char* ret = my_strncat(str1, str2,n); printf("%sn", str1); return 0; } char* my_strncat(char* dest, const char* sour,unsigned n) { char* tmp = dest; assert(dest && sour); //找到目的字符串的' ' while (*dest) { dest++; } while (n)//追加 { *dest++=*sour++; n--; } return tmp;//返回目标空间的地址 }
函数原型:
int strcmp(const char* str1,const char* str2);
#include模拟实现strncmp#include int main() { int my_strcmp(const char*, const char*); const char* str1 = "helloworld"; const char* str2 = "helloboy"; int ret = my_strcmp(str1, str2); printf("%dn", ret);//1 return 0; } int my_strcmp(const char* str1, const char* str2) { assert(str1 && str2); while (*str1 == *str2) { if (*str1 == ' ') { return 0;//等于 } str1++; str2++; } if (*str1 > *str2) return 1;//大于 else return -1;//小于 } 若在gcc编译器上面if-else语句可替换成: return (*str1-*str2);
函数原型:
int strcmp(const char* str1,const char* str2,size_t n);
#define _CRT_SECURE_NO_WARNINGS 1 #include模拟实现strstr#include int main() { int my_strcmp(const char*, const char*, unsigned); const char* str1 = "helloworld"; const char* str2 = "helloboy"; unsigned n = 0; scanf("%u", &n); int ret = my_strcmp(str1, str2, n); printf("%dn", ret);//1 return 0; } int my_strcmp(const char* str1, const char* str2, unsigned n) { assert(str1 && str2); while (n) { if (*str1 == *str2) { str1++; str2++; } else if (*str1 > *str2) return 1;//大于 else return -1;//小于 n--; } return 0; }
函数原型:
char* strstr(const char* str1,const char* str2);
#include模拟实现strchr#include int main() { char* my_strstr(const char*, const char*); char* str1 = "hellowordword";//"hello" "abbbc" char* str2 = "word"; //"hello" "bbc" char* ret = my_strstr(str1, str2); printf("%sn", ret);//wordword return 0; } char* my_strstr(const char* str1, const char* str2) { assert(str1 && str2); char* s1 = (char*)str1; char* s2 = (char*)str2; char* tmp =(char*)str1;//用tmp来存放str2在str1中第一次出现的地址 if (*str2 == ' ') { return (char*)str1; } while (*tmp!=' ') { s1 = tmp; s2 = str2; while((*s1 == *s2) && (*s1!=' ') && (*s2!=' ')) { s1++; s2++; } if (*s2 == ' ') return tmp; tmp++; } return NULL; }
函数原型:
char* strchr(const char* str,char c);
#define _CRT_SECURE_NO_WARNINGS 1 #includechar* my_strchr(const char* str, char c) { while (*str != ' ') { if (*str == c) return (char*)str; str++; } return NULL; } int main() { char* str = "How Do You Do?"; char c; scanf("%c", &c); char* ret = my_strchr(str,c); if (ret != NULL) { printf("%sn", ret); } else printf("NULLn"); return 0; }



