常见字符串函数的实现过程
2017-07-30 20:55
459 查看
一、strlen()函数
求字符串长度,不包括 ‘\0’unsigned int mystrlen( char *s ) { unsigned int len=0; while(*s != '\0') { s++; len++; } return len; }
二、字符串追加函数strcat( ):
char *mystrcat(char *str1,char *str2) { char *p=str1; while(*p) //把p指向最后,检测到\0后退出while循环 p++; while(*p++=*str2++); //空循坏 return str1; }
这里有一个对指针的操作技巧,因为字符串结尾为’\0’(ASCII码为 0),当*p=’\0’时退出while循环。while(*p++=*str2++); 在p的后面追加,一直到str2读取到’\0’,*p=’0’;也可以用数组的方法。
void mystrncat(char data[], char data1[], int n) { int len = strlen(data); int len1 = strlen(data1); int i; for(i = 0; i < n && data1[i] != '\0'; ++i) { data[len + i] = data1[i]; } printf("%s\n", data); }
三、字符串比较函数strcmp( ):
比较是一个一个字符比较。比较ASCII码的大小,比如,如果字符串str1的第一个字符大于str2第一个字符,则str1>str2。#include <stdio.h> #include <string.h> int mystrcmp(const char *s1, const char *s2) { int len1=strlen(s1); int len2=strlen(s2); int i; for(i=0; i<=len1 && i<=len2; i++) { if(s1[i] > s2[i]) { return 1; } if(s1[i] < s2[i]) { return -1; } else if (s1[i] == s2[i]) { return 0; } } }
四、字符串拷贝函数:strcpy( )
和追加不同的是,这里是从str1起始地址开始,用str2覆盖str1。#include <stdio.h> #include <string.h> char *mystrcpy(char *dest, const char *src,int n) { int i; char *p = dest; if(*src ==' ') { printf("src is empty"); return 0; } for(i=0;i<n && src[i]!='\0';i++) { *(p+i)=*(src+i); } return dest; }
五、字符串截取函数:strstr()
返回子串(data1)所在位置的首地址。void mystrstr(char data[], char data1[]) { char* index = NULL; index = data; //主串 char* str1 = NULL; char* str2 = NULL; if(*data1 == '\0') { printf("The data1 is NULL!\n"); } while(*index != '\0') { str1 = data; str2 = data1; while(*str1 != '\0' && *str2 != '\0' && (*str1 - *str2) == 0) //进入while以后,如果能够找到,一直到*str1 !=*str2才会结束循环 { str1++; str2++; } if(*str2 == '\0') //检测到str2结束才打印 { printf("%s\n", index); } index++; //遍历主串 } }
内核代码:
#include <stdio.h> size_t mystrlen(const char * s) { const char *sc; for (sc = s; *sc != '\0'; ++sc) /* nothing */; return sc - s; } char *mystrcpy (char *dest,char *src) { char *temp = dest; while( (*dest++ = *src++) != '\0' ) ; return temp; } char *mystrncpy (char *dest,char *src,size_t count) { char *temp = dest; while ( count-- && (*dest++ = *src++) != '\0') { ; } return temp; } char *mystrcat (char *dest,char *src) { char *temp = dest; while (*dest) { dest++; } while ((*dest++ = *src++) != '\0') { ; } return temp; } int mystrcmp(const char * cs,const char * ct) { register signed char __res; //定义在寄存器中的变量,CPU不必访问内存,能很快的读取 while (1) { if ((__res = *cs - *ct++) != 0 || !*cs++) break; } return __res; } int main() { char arr[10]="abcde"; char a[4]="ach"; char b[10]; printf("%d\n",mystrlen(arr)); //test for strcpy // mystrcpy(b,arr); // printf("%s\n",b); //test for strncpy // mystrncpy(b,arr,3); // printf("%s\n",b); // mystrcat(arr,a); // printf("%s\n",arr); printf("%d\n",mystrcmp(a,arr)); }
相关文章推荐
- 快速排序partition过程常见的两种写法+快速排序非递归实现
- 常见几个字符串函数实现代码
- ListView 实现过程和常见问题
- C语言常见字符串函数实现
- C语言常见字符串函数实现
- 常见字符串函数的模拟实现(二)(c语言)
- 字符串函数模拟实现之strlen函数的三种常见实现方法
- 快速排序partition过程常见的两种写法+快速排序非递归实现
- 常见字符串函数的模拟实现(一)(c语言)
- 常见的字符串函数之strlen函数,strcpy函数,strcat函数的实现;
- 常见字符串函数的实现
- 快速排序partition过程常见的两种写法+快速排序非递归实现
- 几个常见字符串函数的实现
- 几个常见字符串函数的实现
- 常见算法的实现
- JavaScript实现常见排序算法
- Gridview各种功能+AspNetPager+Ajax实现无刷新存储过程分页
- stm32 SysTick定时器常见问题和实现延时
- 关于字符串函数的一些代码实现(strncpy strncat strncmp strchr strrchr)
- _wait_event 具体实现过程