strlen
功能:计算字符串长度【不包含’\0’】
返回值:size_t【无符号整数】
1
| size_t strlen( const char* str)
|
函数实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 方法1: size_t my_strlen(const char *str) { const char *ps = str; while (*ps) { ps++; }
return ps - str; }
方法2: size_t my_strlen(const char *str) { int count = 0; while (*str) { count++; str++; }
return count; }
int main() { char *str = "hello"; int res = my_strlen(str); printf("strlen = %d", res);
return 0; }
|
strcat
1
| char* strcat(char* dest,const char* src)
|
功能:字符串拼接
返回值:返回dest字符串起始地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| char *my_strcat(char *dest, const char *src) { char *p = dest; while (*dest != '\0') { dest++; } while (*dest = *src) { dest++; src++; }
return p; } int main() { char str1[20] = "hello"; char str2[20] = " world"; my_strcat(str1, str2); printf("%s", str1);
return 0; }
|
strcpy
1
| char* strcpy(char* dest,char* src)
|
功能:将参数src字符串拷贝至参数dest所指的地址
返回值:返回参数dest的字符串起始地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 方法1: char* my_strcpy(char* dest, char* src) { char* p = dest; while ((*dest = *src)!='\0') { dest++; src++; }
return p;
}
方法2: char* my_strcpy(char* dest, char* src) { char* p = dest; while (*dest++ = *src++) ;
return p;
}
int main() { char str1[20] = "hello"; char str2[20] = "world"; my_strcpy(str1, str2); printf("%s", str1);
return 0; }
|
strcmp
1
| int strcmp (const char* str1,const char* str2)
|
功能:字符串比较
返回值:若参数s1和s2字符串相同则返回0,s1若大于s2则返回大于0的值,s1若小于s2则返回小于0的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| int my_strcmp(const char *str1, const char *str2) { while ((*str1 != '\0') && (*str1 == *str2)) { str1++; str2++; } return *str1 - *str2; }
int main() { char str1[20] = "hello"; char str2[20] = "hello"; printf("%d", my_strcmp(str1, str2));
return 0; }
|
strstr
1
| char* strstr(const char* str,const char* substr)
|
功能:检索子串在字符串中首次出现的位置
返回值:返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| char *my_strstr(const char *str, const char *substr) { const char *s1; const char *s2; const char *p = str; while (*p) { s1 = p; s2 = substr; while ((*s1 == *s2) && *s2 != '\0') { s1++; s2++; } if (*s2 == '\0') { return (char *)str; } p++; }
return NULL; }
|
reverse
功能:字符串翻转
返回值:void
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| void my_reverse(char *str, int len) { char temp; int start = 0; int end = len - 1; while (start < end) { temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } }
int main() { char str[] = "hello"; int len = sizeof(str) / sizeof(str[0]) - 1; my_reverse(str, len);
for (int i = 0; i < len; i++) { printf("%c", str[i]); } return 0; }
|
const 修饰指针
修饰的是指针指向的内容,保证指针指向的内容不能通过指针来改变。例:const int *pa = &a;
等价于 int const *pa = &a;
【可以改变指针变量指向的地址】
1 2 3 4 5 6 7 8 9 10 11
| int main() { int a = 10; const int *pa = &a; int b = 20; pa = &b; printf("%d\n", *pa); *pa = 6;
return 0; }
|
修饰的是指针变量本身,保证了指针变量的内容不能修改。例:int *const pa = &a;
【不可改变指针变量指向地址】
1 2 3 4 5 6 7 8 9 10 11 12
| int main() { int a = 10; int *const pa = &a; *pa = 20; printf("%d", *pa);
int b = 20; pa = &b;
return 0; }
|