面试必会函数源代码 strcpy/memcpy/atoi/kmp/quicksort
http://blog.csdn.net/liuqiyao_01/article/details/26967813
二、stl模板函数
1、strcpy
- char * strcpy( char *strDest, const char *strSrc )
- {
- if(strDest == strSrc) { return strDest; }
- assert( (strDest != NULL) && (strSrc != NULL) );
- char *address = strDest;
- while( (*strDest++ = * strSrc++) != '\0' );
- return address;
- }
2、strncpy
- <span style="font-size:18px;">char *strncpy(char *strDes, const char *strSrc, unsigned int count)
- {
- assert(strDes != NULL && strSrc != NULL);
- char *address = strDes;
- while (count-- && *strSrc != '\0')
- *strDes++ = *strSrc++;
- *strDes = '\0';
- return address;
- } </span>
3、strcmp
- int strcmp(const char *s, const char *t)
- {
- assert(s != NULL && t != NULL);
- while (*s && *t && *s == *t)
- {
- ++ s;
- ++ t;
- }
- return (*s - *t);
- }
4、strcat
- char *strcat(char *strDes, const char *strSrc)
- {
- assert((strDes != NULL) && (strSrc != NULL));
- char *address = strDes;
- while (*strDes != '\0')
- ++ strDes;
- while ((*strDes ++ = *strSrc ++) != '\0')
- NULL;
- return address;
- }
5、strlen
- int strlen(const char *str)
- {
- assert(str != NULL);
- int len = 0;
- while (*str ++ != '\0')
- ++ len;
- return len;
- }
6、strstr
- char *strstr(const char *strSrc, const char *str)
- {
- assert(strSrc != NULL && str != NULL);
- const char *s = strSrc;
- const char *t = str;
- for (; *strSrc != '\0'; ++ strSrc)
- {
- for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t)
- NULL;
- if (*t == '\0')
- return (char *) strSrc;
- }
- return NULL;
- }
7、strncat
- char *strncat(char *strDes, const char *strSrc, unsigned int count)
- {
- assert((strDes != NULL) && (strSrc != NULL));
- char *address = strDes;
- while (*strDes != '\0')
- ++ strDes;
- while (count -- && *strSrc != '\0' )
- *strDes ++ = *strSrc ++;
- *strDes = '\0';
- return address;
- }
8、strncmp
- int strncmp(const char *s, const char *t, unsigned int count)
- {
- assert((s != NULL) && (t != NULL));
- while (*s && *t && *s == *t && count --)
- {
- ++ s;
- ++ t;
- }
- return (*s - *t);
- }
9、memcpy
- void *memcpy(void *dest, const void *src, unsigned int count)
- {
- assert((dest != NULL) && (src != NULL));
- void *address = dest;
- while (count --)
- {
- *(char *) dest = *(char *) src;
- dest = (char *) dest + 1;
- src = (char *) src + 1;
- }
- return address;
- }
10、memccpy
- void *memccpy(void *dest, const void *src, int c, unsigned int count)
- {
- assert((dest != NULL) && (src != NULL));
- while (count --)
- {
- *(char *) dest = *(char *) src;
- if (* (char *) src == (char) c)
- return ((char *)dest + 1);
- dest = (char *) dest + 1;
- src = (char *) src + 1;
- }
- return NULL;
- }
11、memcmp
- int memcmp(const void *s, const void *t, unsigned int count)
- {
- assert((s != NULL) && (t != NULL));
- while (*(char *) s && *(char *) t && *(char *) s == *(char *) t && count --)
- {
- s = (char *) s + 1;
- t = (char *) t + 1;
- }
- return (*(char *) s - *(char *) t);
- }
12、memmove
- //@big:
- //要处理src和dest有重叠的情况,不是从尾巴开始移动就没问题了。
- //一种情况是dest小于src有重叠,这个时候要从头开始移动,
- //另一种是dest大于src有重叠,这个时候要从尾开始移动。
- void *memmove(void *dest, const void *src, unsigned int count)
- {
- assert(dest != NULL && src != NULL);
- char* pdest = (char*) dest;
- char* psrc = (char*) src;
- //pdest在psrc后面,且两者距离小于count时,从尾部开始移动. 其他情况从头部开始移动
- if (pdest > psrc && pdest - psrc < count)
- {
- while (count--)
- {
- *(pdest + count) = *(psrc + count);
- }
- } else
- {
- while (count--)
- {
- *pdest++ = *psrc++;
- }
- }
- return dest;
- }
13、memset
- void *memset(void *str, int c, unsigned int count)
- {
- assert(str != NULL);
- void *s = str;
- while (count --)
- {
- *(char *) s = (char) c;
- s = (char *) s + 1;
- }
- return str;
- }
三、atoi && itoa
1、atoi
- //代码自己所写,不对地方请及时指正,谢谢!
- //inf用来标记作为判断是否越界
- //atoiFlag作为是否是正确结果的返回值
- const __int64 inf = (0x7fffffff);
- int atoiFlag;
- int atoi(const char* ch)
- {
- ASSERT(ch != NULL);
- atoiFlag = false;
- while (*ch == ' ' || *ch == '\t')
- ch ++;
- int isMinus = 1;
- if (*ch == '-')
- {
- isMinus = -1;
- ch ++;
- }
- else if (*ch == '+')
- {
- ch ++;
- }
- //判断非法
- if (!(*ch <= '9' && *ch >= '0'))
- return 0;
- __int64 ans = 0;
- while (*ch && *ch <= '9' && *ch >= '0')
- {
- ans *= 10;
- ans += *ch - '0';
- //判断越界
- if (ans > inf)
- {
- return inf;
- }
- ch ++;
- }
- ans *= isMinus;
- atoiFlag = true;
- return (int)ans;
- }
- //如何使用
- int main()
- {
- char a[100];
- while (scanf("%s", a) != EOF)
- {
- int ans = atoi(a);
- if (atoiFlag)
- printf("%d\n", ans);
- else if (ans == inf)
- puts("The data of crossing the line");
- else
- puts("Not is a integer");
- }
- return 0;
- }
2、itoa
- #include <stdlib.h>
- #include <stdio.h>
- char *myitoa(int num,char *str,int radix);
- int main()
- {
- int number = -123456;
- char string[25];
- myitoa(number, string, 16);
- printf("integer = %d string = %s\n", number, string);
- return 0;
- }
- /* 实现itoa函数的源代码 */
- char *myitoa(int num,char *str,int radix)
- {
- /* 索引表 */
- char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- unsigned unum; /* 中间变量 */
- int i=0,j,k;
- /* 确定unum的值 */
- if(radix==10&&num<0) /* 十进制负数 */
- {
- unum=(unsigned)-num;
- str[i++]='-';
- }
- else unum=(unsigned)num; /* 其他情况 */
- /* 逆序 */
- do
- {
- str[i++]=index[unum%(unsigned)radix];
- unum/=radix;
- }while(unum);
- str[i]='\0';
- /* 转换 */
- if(str[0]=='-') k=1; /* 十进制负数 */
- else k=0;
- /* 将原来的“/2”改为“/2.0”,保证当num在16~255之间,radix等于16时,也能得到正确结果 */
- char temp;
- for(j=k;j<=(i-k-1)/2.0;j++)
- {
- temp=str[j];
- str[j]=str[i-j-1];
- str[i-j-1]=temp;
- }
- return str;
- }
四、kmp && quicksort
1、kmp
http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html
- void getNext(const char* p, int next[])
- {
- next[0] = -1;
- int index = 0;
- int p_l = strlen(p);
- for (int i=1; i<p_l; i++)
- {
- index = next[i-1];
- while (index >= 0 && p[index+1] != p[i])
- {
- index = next[index];
- }
- if (p[index+1] == p[i])
- {
- next[i] = index + 1;
- }
- else
- {
- next[i] = -1;
- }
- }
- }
- int kmp(const char* t, const char* p)
- {
- const int t_l = strlen(t);
- const int p_l = strlen(p);
- int Next = new int[p_l + 1];
- getNext(p, Next);
- int p_index = 0, t_index = 0;
- int cnt = 0;
- while (p_index < p_l && t_index < t_l)
- {
- if (t[t_index] == p[p_index])
- {
- t_index ++;
- p_index ++;
- }
- else if (p_index == 0)
- {
- t_index ++;
- }
- else
- {
- p_index = Next[p_index-1] + 1;
- }
- if (p_index == p_l)
- {
- cnt ++;
- p_index = Next[p_index-1] + 1;
- }
- }
- delete[] Next;
- return cnt;
- }
2、quicksort
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int partion(int a[], int p, int r)
- {
- srand((unsigned)time(NULL));
- int e = rand()%(r-p+1)+p;
- int tmp = a[e];
- a[e] = a[r];
- a[r] = tmp;
- int x = a[r];
- int i, j;
- i = p;
- for (j=p; j<r; j++)
- {
- if (a[j] <= x)
- {
- tmp = a[i];
- a[i] = a[j];
- a[j] = tmp;
- i ++;
- }
- }
- tmp = a[r];
- a[r] = a[i];
- a[i] = tmp;
- return i;
- }
- void QuickSort(int a[], int p, int r)
- {
- if (p < r)
- {
- int q = partion(a, p, r);
- QuickSort(a, p, q-1);
- QuickSort(a, q+1, r);
- }
- }
- int main()
- {
- int a[] = {1,2,3,-1,5,-9,10,33,0};
- QuickSort(a, 0, 8);
- for (int i=0; i<9; i++)
- {
- printf("%d\t", a[i]);
- }
- return 0;
- }
面试必会函数源代码 strcpy/memcpy/atoi/kmp/quicksort的更多相关文章
- 访谈将源代码的函数 strcpy/memcpy/atoi/kmp/quicksort
一.社论 继上一次发表了一片关于參加秋招的学弟学妹们怎样准备找工作的博客之后,反响非常大.顾在此整理一下,以便大家复习.好多源自july的这篇博客,也有非常多是我自己整理的.希望大家可以一遍一遍的写. ...
- c/c++ 常见字符串处理函数总结 strlen/sizeof strcpy/memcpy/strncpy strcat/strncat strcmp/strncmp sprintf/sscanf strtok/split/getline atoi/atof/atol
这里总结工作中经常用到的一些c/c++的字符串处理方法,标黑的是使用频率较高的 1.strlen函数:计算目标字符串长度, 格式:strlen(字符指针指向区域) 注意1:①不包含字符串结束 ...
- C/C++自实现的函数(memset, memcpy, atoi)
函数原型: void * memset ( void * buffer, int c, size_t num ); 关于void * 因为任何类型的指针都可以传入memset函数,这也真是体现了内存操 ...
- linux驱动工程面试必问知识点
linux内核原理面试必问(由易到难) 简单型 1:linux中内核空间及用户空间的区别?用户空间与内核通信方式有哪些? 2:linux中内存划分及如何使用?虚拟地址及物理地址的概念及彼此之间的转化, ...
- python3全栈开发- 元类metaclass(面试必考题)
一.知识储备 #exec:三个参数 #参数一:字符串形式的命令 #参数二:全局作用域(字典形式),如果不指定,默认为globals() #参数三:局部作用域(字典形式),如果不指定,默认为locals ...
- MySQL面试必考知识点:揭秘亿级高并发数据库调优与最佳实践法则
做业务,要懂基本的SQL语句: 做性能优化,要懂索引,懂引擎: 做分库分表,要懂主从,懂读写分离... 数据库的使用,是开发人员的基本功,对它掌握越清晰越深入,你能做的事情就越多. 今天我们用10分钟 ...
- 面试必会之HashMap源码分析
相关文章 面试必会之ArrayList源码分析 面试必会之LinkedList源码分析 简介 HashMap最早出现在JDK1.2中,底层基于散列算法实现.HashMap 允许 null 键和 nul ...
- Java BAT大型公司面试必考技能视频-1.HashMap源码分析与实现
视频通过以下四个方面介绍了HASHMAP的内容 一. 什么是HashMap Hash散列将一个任意的长度通过某种算法(Hash函数算法)转换成一个固定的值. MAP:地图 x,y 存储 总结:通过HA ...
- 互联网公司面试必问的Redis题目
Redis是一个非常火的非关系型数据库,火到什么程度呢?只要是一个互联网公司都会使用到.Redis相关的问题可以说是面试必问的,下面我从个人当面试官的经验,总结几个必须要掌握的知识点. 介绍:Redi ...
随机推荐
- python学习笔记6--mockserver
一.mockserver的应用 有时候测试我们需要调用一些三方接口或者未开发完成的接口,完成我们的业务流程测试,但是这时候可能我们只知道接口返回值,接口并没有完全开发完成或可以让我们任意调用,这时候就 ...
- 消息队列之RabbitMQ的.Net客户端EasyNetQ
https://www.cnblogs.com/CoderAyu/p/9072408.html http://www.cnblogs.com/panzi/p/6337568.html http://w ...
- RabbitMq简单应用
服务端: <?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => ...
- CodeForces Contest #1110: Global Round 1
比赛传送门:CF #1110. 比赛记录:点我. 涨了挺多分,希望下次还能涨. [A]Parity 题意简述: 问 \(k\) 位 \(b\) 进制数 \(\overline{a_1a_2\cdots ...
- centos7执行 wget命令: command not found的两种解决方法
1.rpm 安装 下载wget的RPM包: http://mirrors.163.com/centos/6.8/os/x86_64/Packages/wget-1.12-8.el6.x86_64.rp ...
- Extjs Window用法详解 3 打印具体应用,是否关掉打印预览的界面
Extjs Window用法详解 3 打印具体应用,是否关掉打印预览的界面 Extjs 中的按钮元素 {xtype: 'buttongroup',title: '打印',items: [me.ts ...
- 证书(Certificate)与描述文件(Provisioning Profiles)
在使用脚本xcodebuild自动打包的时候,会用到签名证书和描述文件,我在编译的时候搞了好长时间才搞明白,下面介绍如何得到正确配置. 证书:证书是用来给应用程序签名的,只有经过签名的应用程序才能保证 ...
- java 异常与记录日志
一. 你可能还想利用java.util.logging工具将输出记录到日志中 package exceptions; //: exceptions/LoggingExceptions.java // ...
- POJ 3666 Making the Grade(二维DP)
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
- **CI创建类库(创建自己的工具类等)
创建类库 当我们使用术语"类库"时,我们一般指的是位于libraries 文件夹中的类,它们在wiki的"类库参考"这个板块被讨论.在当前这个话题中,我们将讨论 ...