常见字符串函数的实现

来源:互联网 发布:php微信抢红包源代码 编辑:程序博客网 时间:2024/05/21 14:47

strlen(),strcpy()

int my_strlen(const char *str)//字符串长度{    assert(str != NULL);           int len = 0;       while (*str ++ != '\0')    {        ++ len;    }     return len;   }char *my_strcpy(char *di,const char *si)//字符串的拷贝{    assert((di != NULL) && (si != NULL));    char * cp = di;    while(*si)    {        *di++ = *si++;    }    *di= '\0';    return cp;       }void main(){    char str[100]="";    char *dtr="hello word";    int n=my_strlen(dtr);    char *p=my_strcpy(str,dtr);    printf("字符串长度:%d\n",n);    printf("字符串拷贝后:%s\n",p);}

strncpy(),strcat(),strncat()

char *my_strncpy(char *di, const char *si, int n)//字符串的长度受限拷贝{    assert((di != NULL) && (si != NULL));    char *ncpy = di;    while(n--)    {        if(*si != '\0')        {            *di++ = *si++;        }        else        {            *di++ = '\0';        }    }    return ncpy;       }char *my_strcat(char *di, const char *si)//两个字符串的连接 {       assert((di != NULL) && (si != NULL));       char *link = di;       while (*di != '\0')    {        ++ di;    }            while ((*di ++ = *si ++) != '\0')    {    }    return link;   }char *my_strncat(char *di, const char *si, int n) // 两个字符串的受限连接{       assert((di != NULL) && (si != NULL));       char *nlink = di;       while (*di != '\0')    {        ++ di;     }      while (n-- && *si != '\0' )    {          *di ++ = *si ++;    }       *di = '\0';       return nlink;   } void main(){    char str[100]="";    char ctr[100]="ff";    char ftr[100]="ff";    char *dtr="hello word";    printf("字符串长度受限拷贝后:%s\n",my_strncpy(str,dtr,5));    printf("字符串连接后:%s\n",my_strcat(ctr,dtr));    printf("字符串受限连接后:%s\n",my_strncat(ftr,dtr,7));}

strchr(),strrchr(),strnchr()

char *my_strchr( char *di, char c)// 查找字符串中首次出现字符c的位置,找到返回指针,找不到返回NULL {       assert(di != NULL);       for (; *di !=c; ++ di)    {          if (*di == '\0')         {            return NULL;        }       }    return di;   }char *my_strrchr( char *si, char c)//查找字符串中最后一次次出现字符c的位置,找到返回指针,找不到返回NULL {    assert(si != NULL);    char *p = NULL;    while(*si != '\0')    {        if(*si == (char)c)        {            p = (char *)si;        }        si++;    }    return p;}char *my_strnchr( const char *di,char c,int n)//字符串查找给定字符,第 n 次出现的位置{    assert(di != NULL);     int count=0;    for (; *di !='\0'; ++ di)    {          if (*di==c)        {            count+=1;        }        if(count==n)        {        return (char *)di;        }    }    return NULL;}void main(){    char *str="hello a waord";    char *ctr="hello a waard";    printf("前查找字符后:%s\n",my_strchr(str,'a'));    printf("后查找字符后:%s\n",my_strrchr(str,'a'));    printf("查找第n次字符出现后:%s\n",my_strnchr(ctr,'a',3));}

strstr()

char *my_strstr(const char *di ,const char *si)//查找字符串(子串)   {       assert(di != NULL && si != NULL);       const char *s = di;       const char *t = si;       for (; *di != '\0'; ++ di)       {           for (s = di, t = si; *t != '\0' && *s == *t; ++s, ++t)        {               NULL;        }           if (*t == '\0')        {               return (char *) di;        }       }       return NULL;} void main(){    char *str="hello word";    char *ctr="word";    printf("%s\n",my_strstr(str,ctr));}

strcmp(),strncmp()

int my_strcmp(const char *di, const char *si) //字符串比较,返回负,零,正数  {       assert(di != NULL && si != NULL);       while(*di && *si && *di == *si)       {           ++ di;           ++ si;       }    return (*di - *si);   }int my_strncmp(const char *di, const char *si, int n)//受限字符串的比较  {       assert((di != NULL) && (si != NULL));       while (*di && *si && *di == *si && n --)       {           ++ di;           ++ si;       }       return(*di - *si);   }void main(){    char *str="hellobword";    char *ctr="helloaword";    printf("%d\n",my_strcmp(str,ctr));    printf("%d\n",my_strncmp(str,ctr,4));}

count_chars()

int count_chars(char const *di,char const *si)//在第一个参数中进行查找, 并返回匹配第二个参数所包含的字符的数量 {      assert((di != NULL) && (si != NULL));    int count = 0;      for (int i = 0;i < strlen(di);i++ )      {          for(int j = 0;j < strlen(si);j++)          {              if( di[i] == si[j])              {                  count++;              }          }      }      return count;  } void main(){    char *str="abcdefg";    char *ctr="acdfyuilo";    printf("相同字符数:%d\n",count_chars(str,ctr));}
阅读全文
'); })();
1 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 朗动2017款报价及图片 2015款朗动 朗动 现代朗动2013款 朗动怎么样 古代名画 世界名画欣赏500幅 名画图片 名画赏析 名画欣赏 名城苏州 大名城 大名城股票 大名城股吧 名城苏州网 大壮名城 历史名城 兰州大名城 苏州名城 名城淮安网 鑫苑名城 苏州名城网 名城 名城大学 东方名城 文化名城 汇融名城 晓港名城 名城银河湾 名城天下 名城广场 玫瑰名城 世界名城 水岸名城 学府名城 名城老窖 和源名城 历史文化名城 七彩古滇名城 大名城股票股吧 历史文化名城保护规划