【经典】C语言字符串函数原型

来源:互联网 发布:防小三软件 编辑:程序博客网 时间:2024/04/28 08:00

strlen / strcmp / strcat / strcpy / memset / strstr / atoi / itoa /

/** 代码演示 -strlen.c **/
size_t mystrlen2 (char *s) // 内核
{
 char *p = s;
 while (*p)
  p++;
 return p - s;
}

/** 代码演示 -strcmp.c **/
int mystrcmp (char *s1, char *s2)
{
 char *p = s1;
 while (*p) {
  if (*p > *s2)
   return 1;
  if (*p < *s2)
   return -1;
  p++;
  s2++;
 }
 return *s2 == 0 ? 0 : -1;
}

/** 代码演示 -strcat.c **/
char *mystrcat (char *s1, char *s2)
{
 char *p = s1;
 while (*s1)
  s1++;
 while (*s1++ = *s2++);
 return p; // 首地址
}

/** 代码演示 -strcpy.c **/
char *mystrcpy (char *dest, char *src)
{
 char *p = dest;
 while (*dest++ = *src++);
 return p;
}

/** 代码演示 -memset.c **/ 
void *mymemset (void *s, char c, size_t len)
{
 char *p = s;
 while (len--)
  *p++ = c;
 return s;
}

/** 代码演示 - strstr.c **/
const char *mystrstr (const char *s, const char *sub_s)
{
    int i, j;
    int tmp = 0;
    for (i = 0; s[i] != '\0'; i++) { // i遍历主串,包含子串即返回i下标位置
        j = 0; // 子串位置每次从头开始
        while (s[tmp++] == sub_s[j++]) { // tmp遍历主串,主做判断下标0到字符串末尾\0
            if (sub_s[j] == '\0')
                return &s[i];
        }  
    }  
    return NULL;
}


/** 代码演示 - itoa.c **/

void itoa (char* buf, unsigned int num) {
    unsigned int tmp = 0;
    buf[0] = '0';
    buf[1] = 'x';
    int i = 9; 
    while (num) {
        tmp = num % 16;
        if (tmp > 9)
            buf[i] = 'a' + tmp - 10;
        else
            buf[i] = '0' + tmp;
        num /= 16;
        i--;
    }
    while (i >= 2) {
        buf[i--] = '0'; // 0x0000005a 中间补全的0
    }
    buf[10] = 0; // 最后1位的\0
}

/* itoa.c 版本2 - 原理一样 */
#include <stdio.h>
#define ARR_SIZE 20
void myitoa (char *dest, int num)
{
    int i = 0, j = 0;
    while (num) {
        dest[i++] = num % 10 + '0';
        num /= 10;
    }  
    dest[i] = '\0';
    for (j = i; j >= 0; j--)
        printf ("%c", dest[j]);
    printf ("\n");
}

int main ()
{
    int num = 0;
    char str[ARR_SIZE] = {0};
    printf ("输入一个数字:");
    scanf ("%d", &num);
    myitoa (str, num);
    printf ("转换为字符串:%s\n", str);
    return 0;
}

/** 代码演示 - atoi.c **/
int atoi (char* s) { 
    int num = 0;
    char* tmp = s;
    while (*tmp++) {
        if (*tmp >= '0' && *tmp <= '9')
            num = num * 10 + (*tmp - '0'); // '0' <==> 48
        else
            break;
    }  
    return num;
}


/** 代码演示 - atoi.c  - 版本2  **/
int myAtoi(const char * str)
{
    int num = 0; //保存转换后的数值
   int isNegative = 0; //记录字符串中是否有负号
   int n =0;
   char *p = str;
   if(p == NULL) //
判断指针的合法性
   {
         return -1;
   }
   while(*p++ != '\0') //
计算数字符串度
   {
         n++;
   }
   p = str;
   if(p[0] == '-') //
判断数组是否有负号
   {
         isNegative = 1;
   }
   char temp = '0';
   for(int i = 0 ; i < n; i++)
   {
        char temp = *p++;
        if(temp > '9' ||temp < '0') //滤除非数字字符
        {
           continue;
        }
         if(num !=0 || temp != '0') //滤除字符串开始的0字符
        {
            temp -= 0x30; //将数字字符转换为数值
            num += temp *int( pow(10 , n - 1 -i) );
         }
   }
   if(isNegative) //
如果字符串中有负号,将数值取反
   {
         return (0 - num);
   }
   else
   {
        return num; //返回转换后的数值
   }
}
注意:此段代码只是实现了十进制字符串到数字的转化,读者可以自己去实现2进制,8进制,10进制,16进制的转化。


/** 字符串倒序:倒序一个字符串代码演示 **/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
    char *src = "hello";
    char *dest = NULL;
    int len = strlen (src);
    dest = (char *)malloc (len+1);
    char *d = dest;
    char *s = &src[len-1];
    while (len-- != 0) 
        *d++ = *s--;
    *d = '\0';
    printf ("dest = %s\n", dest);
    free (dest);
    dest = NULL;
    return 0;
}


判断是否是回文函数:
bool fun(char *p)
{
    int len = strlen(p);
    char *q = p + len - 1; 
    while (p < q) { // 当两个指针指向的是同一个数组则可以比较大小 
        if ((*p++) != (*q--))
        return flase;
    }
    return true;
}

0 0