版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_42719751/article/details/86665416
常见字符串操作函数的实现
strlen函数
- 函数声明原型:
size_t strlen( const char *string );
- 函数功能:
计算指定的字符串string的长度。 - strlen函数实现:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
size_t strlen(const char *string);
size_t my_strlen1(const char* string)
{
assert(string);
size_t count = 0;
while (*string++)
{
count++;
}
return count;
}
size_t my_strlen2(const char* string)
{
assert(string);
if (*string == '\0')
return 0;
return 1 + my_strlen2(string + 1);
}
int main()
{
char s[] = "abcdefg";
printf("%d\n", my_strlen2(s));
system("pause");
return 0;
}
strcpy函数
- 函数声明原型:
char *strcpy( char *strDestination, const char *strSource );
- 函数功能:
将参数src字符串拷贝至参数dest所指的地址。 - 函数实现:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* src)
{
assert(dest);
assert(src);
char* ptr = dest;
while (*src)
{
*dest++ = *src++;
}
*dest = *src;
return ptr;
}
int main()
{
char s1[] = "abefg";
char s2[] = "cdefg";
printf("%s\n", my_strcpy(s1, s2));
system("pause");
return 0;
}
strcmp函数
- 函数声明原型:
int strcmp( const char *string1, const char *string2 );
- 函数功能:
字符串比较。 - 函数实现:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int strcmp(const char *string1, const char *string2);//库函数原型
int my_strcmp(const char* s1, const char* s2)
{
assert(s1);
assert(s2);
char* e1 = s1;
char* e2 = s2;
while ((*e1 == *e2) && (*e1 != '\0') && (*e2 != '\0'))
{
e1++;
e2++;
}
if (*e1 > *e2)
return 1;
else if (*e1 < *e2)
return -1;
if (*e1 == *e2 == '\0')
return 0;
}
int main()
{
char s1[] = "bcde";
char s2[] = "cde";
printf("%d\n", my_strcmp(s1, s2));
system("pause");
return 0;
}
strcat函数
- 函数声明原型:
char *strcat( char *strDestination, const char *strSource );
- 函数功能:
字符串拼接函数。 - 函数实现:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest);
assert(src);
char* ptr = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++);
*dest = *src;
return ptr;
}
int main()
{
char s1[] = "abcdef";
char s2[] = "bcd";
printf("%s\n",my_strcat(s1, s2));
system("pause");
return 0;
}
strchr函数
- 函数声明原型:
char *strchr( const char *string, int c );
- 函数功能:
在一个字符串中查找给定字符的第一个匹配之处。 - 函数实现:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
char* my_strchr(const char* dest, int c)
{
assert(dest);
char* ptr = dest;
while (*ptr++ != c);
if (*ptr != '\0')
return --ptr;
else
return NULL;
}
int main()
{
char s1[] = "abcdef";
char c = 'n';
printf("%s\n", my_strchr(s1, c));
system("pause");
return 0;
}
strstr函数
- 函数声明原型:
char *strstr( const char *string, const char *strCharSet );
- 函数功能:
检索子串在字符串中首次出现的位置。 - 函数实现:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char* my_strstr(char* dest, const char* src)
{
assert(dest);
assert(src);
char* ptr1 = dest;
char* ptr2 = NULL;
while (*ptr1 != *src && *ptr1 != '\0')
{
ptr1++;
ptr2 = ptr1;
}
while (*ptr1 == *src)
{
ptr1++;
src++;
}
if (*src != '\0')
return NULL;
else
return ptr2;
}
int main()
{
char s1[] = "abcdefg";
char s2[] = "mn";
printf("%s\n", my_strstr(s1, s2));
system("pause");
return 0;
}
strncpy函数
- 函数声明原型:
char *strncpy( char *strDest, const char *strSource, size_t count );
- 函数功能:
将字符串src前n个字符拷贝到字符串dest。 - 函数实现:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
char* my_strncpy(char* dest, const char* src, size_t count)
{
assert(dest);
assert(src);
char* ptr = dest;
while (count-- && *src != '\0')
{
*ptr++ = *src++;
}
if (*src == '\0')
*ptr = '\0';
return dest;
}
int main()
{
char s1[] = "abcd";
char s2[] = "efg";
printf("%s\n", my_strncpy(s1, s2, 2));
system("pause");
return 0;
}
strncat函数
- 函数声明原型:
char *strncat( char *strDest, const char *strSource, size_t count );
- 函数功能:
将n个字符追加到字符串的结尾。 - 函数实现:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char* my_strncat(char* dest, const char* src, size_t count)
{
assert(dest);
assert(src);
char* ptr = dest;
while (*ptr++ != '\0');
ptr--;
while (count-- && *src != '\0')
{
*ptr = *src;
ptr++;
src++;
}
*ptr = '\0';
return dest;
}
int main()
{
char s1[] = "abcd";
char s2[] = "efgh";
printf("%s\n", my_strncat(s1, s2, 5));
system("pause");
return 0;
}
strncmp函数
- 函数声明原型:
int strncmp( const char *string1, const char *string2, size_t count );
- 函数功能:
字符串前n个字符比较。 - 函数实现:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int my_strncmp(const char* s1, const char* s2, size_t count)
{
assert(s1);
assert(s2);
while ((count--) && (*s1 != '\0') && (*s2 != '\0'))
{
if (*s1 > *s2)
return 1;
if (*s1 < *s2)
return -1;
s1++; s2++;
}
if (*s1 != '\0')
return 1;
if (*s2 != '\0')
return -1;
return 0;
}
int main()
{
char s1[] = "bcd";
char s2[] = "bcdd";
printf("%d\n", my_strncmp(s1, s2, 5));
system("pause");
return 0;
}
assert()函数用法
希望博主的这篇文章可以帮到你,,加油!!!