C字符串操作函数的实现详细解析

所属分类: 软件编程 / C 语言 阅读数: 94
收藏 0 赞 0 分享

1. strlen(),计算字符串长度  

int strlen(const char string)  
{  
  int i=0;  
  while(string[i]) i++;  
  return i;  
}  

2. strcpy(), 字符串拷贝.   

char *strcpy(char *destination, const char *source)  
{  
  while(*destinaton++=*source++);  
  return (destination-1);  
}

3. strcat(), 字符串的连接.   

char *strcat(char *target,const char *source)  
{  
  char *original=target;  
  while(*target) target++; // Find the end of the string  
  while(*target++=*source++);  
  return(original);  
} 

4. streql(), 判断两个字符串是否相等.   

int streql(char *str1,char *str2)  
{  
  while((*str1==*str2)&&(*str1))  
  {  
    str1++;  
    str2++;  
  }  
  return((*str1==NULL)&&(*str2==NULL));  
} 

5. strchr(), 在字符串中查找某个字符.   

char *strchr(const char *string,int letter)  
{  
  while((*string!=letter)&(*string))  
    string++;  
  return (string);  
}  

6. chrcnt(), 计算某个字符在字符串中出现的次数.  

int chrcnt(const char *string,int letter)  
{  
  int count=0;  
  while(*string)  
    if(*string==letter)count++;  
  return count;  
} 

7. strcmp(), 判断两个字符串是否相等.  

int strcmp(const char *str1,const char *str2)  
{  
  while((*str1==*str2)&&(*str1))  
  {  
    str1++;  
    str2++;  
  }  
  if((*str1==*str2)&&(!*str1)) //Same strings  
    return o;  
  else if((*str1)&&(!*str2)) //Same but str1 longer  
    return -1;  
  else if((*str2)&&(!*str1)) //Same but str2 longer  
  else  
  return((*str1>*str2)?-1:1);  
}

更多精彩内容其他人还在看

从汇编看c++中变量类型的深入分析

本篇文章是对c++中的变量类型进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

从汇编看c++的默认析构函数的使用详解

本篇文章是对c++中默认析构函数的使用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

基于c++中的默认拷贝函数的使用详解

本篇文章对c++中默认拷贝函数的使用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

解析c++中的默认operator=操作的详解

本篇文章是对c++中的默认operator=操作的应用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

解析c++中参数对象与局部对象的析构顺序的详解

本篇文章是对c++中参数对象与局部对象的析构顺序进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入c++中临时对象的析构时机的详解

本篇文章对c++中临时对象的析构时机进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解析内存对齐 Data alignment: Straighten up and fly right的详解

对于所有直接操作内存的程序员来说,数据对齐都是很重要的问题.数据对齐对你的程序的表现甚至能否正常运行都会产生影响
收藏 0 赞 0 分享

深入内存对齐的详解

本篇文章是对内存对齐进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入C语言把文件读入字符串以及将字符串写入文件的解决方法

本篇文章是对C语言把文件读入字符串以及将字符串写入文件的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入Windows下的回车是回车换行(\r\n)还是换行回车(\n\r)的详解

本篇文章对Windows下的回车是回车换行(\r\n)还是换行回车(\n\r)进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多