在书本上看到的关于字符串的一些算法
#include <stdio.h>
#include <string.h>#include <assert.h>
#include <malloc.h>
#define MAXLEN 100
#define N 80
//my_strlen
//计算字符串的长度函数的实现
int my_strlen(char *str)
{
int i=0;
char *pstr = 0;
pstr = str;
while(pstr[i])
{
i++;
}
return i;
}
//strcpy
//字符串的拷
void my_strcpy(char *des,char *scr)
{
assert(des!=NULL&&scr!=NULL);
while(*scr)
{
*des++=*scr++;
}
*des='\0';
}
//isalpha
//判断字符是否为字母函数的实现
int my_isalpha(int c)
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
{
return 1;
}
else
return 0;
}
//isdigit
//判断字符是否为数字函数的实现
int my_isdigit(int c)
{
if(c>='0'&&c<='9')
{
return 1;
}
else
return 0;
}
//islower
//判断字符是否为小写字母函数的实现
int my_islower(int c)
{
if(c>='a'&&c<='z')
{
return 1;
}
else
return 0;
}
//tolower
//判断字符是否为大写字母函数的实现
char my_tolower(int c)
{
if(c>='A'&& c<='Z')
{
c=c-32+'A'-1;
return c;
}
else
{
return c;
}
}
//toupper
//将小写字母转化大写字母
char my_toupper(int c)
{
//判断字符是否为字母
if(c>='A'&&c<='z')
{
//判断字符是否为小写字母
if(c>='a'&&c<='z')
{
//将小写字母转化大写字母
c=c-'a'+32;
return c;
}
else
{
return c;
}
}
else
{
return c;
}
}
//strchar
//在字符串中查找某个字符
char *my_strchr(char *str,int c)
{
while((*str!=c)&&(*str))
{
str++;
}
return (str);
}
//strcmp
//比较字符串大小函数的实现
int my_strcmp(char *s1,char *s2)
{
int diffent = 0;
while(1){
if(*s1 == '\0' && *s2 == '\0'){
diffent = 0;
break;
}
diffent = *s1 - *s2;
if(diffent){
break;
}
s1++;
s2++;
}
return diffent;
}
//strcat
//字符串的连接函数的实现
char *my_strcat(char *target,const char *source)
{
char *original=target;
while(*original != '\0')
original++; // Find the end of the string
while(*source != '0')
*original++=*source++;
*original='\0';
return(target);
}
//chrcnt
//计算某个字符在字符串出现的次数函数的实现
int my_chrcnt(char *sour,int letter)
{
int count=0;
while(*sour)
{
if(*sour==letter)
count++;
sour++;
}
return count;
}
//字符串删除算法
int my_del(