面试必会函数源代码 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 ...
随机推荐
- 第12月第29天 cocos quick manual
1. Example: $ cd cocos2d-x $ ./setup.py $ source FILE_TO_SAVE_SYSTEM_VARIABLE $ cocos new MyGame -p ...
- crontab每10秒钟执行一次
1.使用sleep 在crontab中加入 * * * * * sleep 10; /bin/date >>/tmp/date.txt* * * * * sleep 20; /bin/da ...
- appium-Could not obtain screenshot: [object Object]
原因 App页面已经被禁止截屏,禁用用户截屏的代码如下: getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); setConten ...
- web.js
var page = require('webpage').create(), system = require('system'), address,output,csvPath,nodePathF ...
- kibana提示"[illegal_argument_exception] mapper [hits] cannot be changed from type [long] to [integer]"
=============================================== 2019/1/30_第1次修改 ccb_warlock == ...
- liunx jdk安装
打开https://www.oracle.com/technetwork/java/javase/downloads/index.html 选择Development版本(server为服务器版本), ...
- Servlet 3.0 新特性详解
转自:http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/#major3 Servlet 是 Java EE 规范体系的重要组成部分,也是 ...
- 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)(转)
前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...
- Oracle数据库游标,序列,存储过程,存储函数,触发器
游标的概念: 游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理, ...
- React Native Android启动白屏的一种解决方案上
我们用RN去开发Android应用的时候,我们会发现一个很明显的问题,这个问题就是启动时每次都会有1~3秒的白屏时间,直到项目加载出来 为什么会出现这个问题? RN开发的应用在启动时,首先会将js b ...