zoukankan      html  css  js  c++  java
  • 【面试题目】字符串函数汇总

     1 //求字符串长度
     2 int strlen(const char *s) {
     3     int n = 0;
     4     while (*s++ != '\0')
     5         n++;
     6     return n;
     7 }
     8 
     9 //字符串拷贝,返回指针是为了实现链式操作,如strlen(strcpy(dst, src)),dst需要保证有足够空间
    10 char *strcpy(char *dst, const char *src) {
    11     assert(dst != NULL && src != NULL); //拷贝空串无意义
    12     if(dst == src) 
    13             return dst;
    14     char *ret = dst;
    15     while ((*dst++ = *src++) != '\0')
    16         ;
    17     return ret;
    18 }
    19 
    20 //字符串比较,p>q则返回整数,p=q返回0,否则返回负数
    21 int strcmp(const char *p, const char *q) {
    22     while (*p && *p == *q)
    23         p++, q++;
    24     return (int) ((unsigned char) *p - (unsigned char) *q);
    25 }
    26 
    27 // 找到字符c在字符串s中首次出现的位置
    28 char *strchr(const char *s, char c) {
    29     for (; *s; s++)
    30         if (*s == c)
    31             return (char *) s;
    32     return NULL;
    33 }
    34 
    35 //拼接字符串,注意这里dst需要保证有足够的空间
    36 char *strcat(char *dst, const char *src) {
    37     char *d = dst;
    38     while (*d)
    39         d++;
    40     while ((*d = *src) != '\0')
    41         ;
    42     return dst;
    43 }
    44   

     

    转自:http://blog.csdn.net/ssjhust123/article/details/7868188

  • 相关阅读:
    代理模式
    面向对象设计原则
    砝码破碎
    阿里EasyExcel使用
    IBM的OpenJ9
    java反射 (复习)
    DecimalFormat保留小数
    Object类
    SQLMAP用法
    SQL盲注之时间注入
  • 原文地址:https://www.cnblogs.com/dracohan/p/2873354.html
Copyright © 2011-2022 走看看