/*================================================================
* Copyright (C) 2022 HSW_study Ltd. All rights reserved.
*
* 文件名称:string.c
* 创 建 者:HSW
* 创建日期:2022年03月13日
* 描 述:
*
================================================================*/
//编写一个标准strcpy函数
#include <stdio.h>
#include <assert.h>
#include <string.h>
//原型声明:char *strcpy(char* dest, const char *src);
char *my_strcpy(char *StrDest,const char *StrSrc)
{
assert((NULL != StrDest)&&(NULL != StrSrc));
char *str = StrDest;
while((*StrDest++ = *StrSrc++)!='\0');
return str;
}
//编写出一个标准的strlen函数
//原型声明:size_t strlen(const char* str);
//字符串结束符'\0'为止,返回计数器值(长度不包含'\0')
size_t mystrlen(const char *str)
{
assert(NULL != str);
int count=0;
while((*str++) != '\0')
{
count++;
}
return count;
}
//编写一个strcat函数
//char *strcat(char *dest, const char *src);
char *my_strcat(char *dest, const char *src)
{
assert((NULL!=dest)&&(NULL!=src));
char *str = dest;
while(*str)
str++;
while((*str++ = *src++));
return dest;
}
//编写一个标准strcmp函数
//原型声明:int strcmp(const char *s1, const char *s2);
int my_strcmp(const char *s1,const char *s2)
{
while(*s1==*s2)
{
assert((s1!=NULL)&&(s2!=NULL));
if(*s1=='\0')
return 0;
s1++;
s2++;
}
return *s1-*s2;
}
//编写一个strstr函数
//函数声明:char *strstr(const char *haystack, const char *needle);
char *my_strstr(const char *str1,const char *str2)
{
char *cp = (char *)str1;
char *s1,*s2;
if(!*str2)
return ((char *)str1);
while(*cp)
{
s1 = cp;
s2 = (char *)str2;
while(*s1 && *s2 && !(*s1-*s2))
s1++,s2++;
if(!*s2)
return cp;
cp++;
}
return NULL;
}
//编写一个自己的完全C语言版本的memset函数
//函数声明:void *memset(void *s, int c, size_t n);
void *my_memset(void *s,int c,unsigned int n)
{
const unsigned char uc = c;
unsigned char *su = s;
for (su = s ; 0 < n; --n,su++) {
*su = uc;
}
return s;
}
//编写一个memcpy函数
//函数声明: void *memcpy(void *dest, const void *src, size_t n);
void *my_memcpy(void *dest,const void *src,unsigned int n)
{
assert((NULL!=dest)&&(NULL!=src));
void *str = dest;
if(dest <= src || (char *)dest >= (char * )src + n)
{
printf("没有内存重叠,从低地址开始复制\n");
while(n--){
*(char *)str = *(char *)str;
str = (char *)str + 1;
src = (char *)src + 1;
}
}else{
printf("有内存重叠,从高地址开始复制\n");
str = (char *)str + n - 1;
src = (char *)src + n - 1;
while(n--){
str = (char *)str - 1;
src = (char *)src - 1;
}
}
return str;
}
//功 能: 把字符串转换成长整型数
//用 法: long atol(const char *nptr);
long my_atol(const char *nptr)
{
int num = 0;
while(*nptr)
{
num = 10 * num + *nptr-'0';
nptr++;
}
return num;
}
//编写一个字符串逆转函数
char *reversal_string(char *str)
{
assert(NULL != str);
char *cp = str;
char temp;
int n = strlen(str)-1;
char *pstr = & cp[n];
while(cp < pstr)
{
temp = *cp;
*cp = *pstr;
*pstr = temp;
cp++;
pstr--;
}
return cp;
}
//world翻转
char *reversal_str_word(char *str,char *pstr)
{
assert(NULL != str);
char temp;
while(str<pstr)
{
temp = *str;
*str = *pstr;
*pstr = temp;
str++;
pstr--;
}
return str;
}
char *reversal_word(char *str)
{
assert(NULL != str);
char *cp = str;
int n = strlen(str)-1;
char *pstr = &cp[n];
reversal_str_word(cp,pstr);
while(*cp)
{
char *start = cp;
while(*cp != ' ' && *cp != '\0')
cp++;
reversal_str_word(start,cp-1);
cp ++;
}
return str;
}
string.h库函数编写,strcpy、strlen、strstr、strcat、strcmp、memset、memcpy、atol、字符串反转、单词反转
最新推荐文章于 2024-08-14 23:33:02 发布