char* strcpy( char* dest, const char* src ), int binary_search(int *arr, int key, int n), 可能的实现
#include <stdio.h>
char* stringCopy( char* dest, const char* src )
{
size_t i = 0;
while (dest[i] = src[i++]);
return dest;
}
int binary_search(int *arr, int key, int n)
{
int i = n / 2;
if (arr[i] < key)
{
for ( ++i; i < n; ++i)
{
if (arr[i] == key) return i;
}
}
else
{
for (; i > -1; --i)
{
if (arr[i] == key) return i;
}
}
return -1;
}
int main()
{
char strBuf[100];
char *ret = stringCopy(strBuf, "This is a test!\n这是一个测试!");
printf("%s\n", strBuf);
printf("%s\n", ret);
int ints[] = { 1, 3, 5, 7, 9 };
printf("%d\n", binary_search(ints, 5, sizeof ints / sizeof(int)));
printf("%d\n", binary_search(ints, -1, sizeof ints/ sizeof(int)));
printf("%d\n", binary_search(ints, 9, sizeof ints/ sizeof(int)));
printf("%d\n", binary_search(ints, 3, sizeof ints/ sizeof(int)));
return 0;
}
char* strcpy( char* dest, const char* src ), int binary_search(int *arr, int key, int n), 可能的实现的更多相关文章
- char * p = "abc"与const char *p = "abc"
char * p = "abc"与const char *p = "abc"的区别是什么呢? 第一个语句会产生问题: warning: deprecated c ...
- void *memmove( void* dest, const void* src, size_t count );数据拷贝,不需要CPU帮助
分享到 腾讯微博 QQ空间 新浪微博 人人网 朋友网 memmove 编辑词条 编辑词条 --> memmove用于从src拷贝count个字符到dest,如果目标区域和源区域有重叠的话,m ...
- 用C语言实现一个公用库函数void * memmove(void *dest,const void *src,size_t n)
用C语言实现一个公用库函数void * memmove(void *dest,const void *src,size_t n). 该函数的功能是拷贝src所指的内存内容前n个字节到dest所指的地址 ...
- 字符串复制char *strcpy(char* dest, const char *src);
⒈strcpy的实现代码 char * strcpy(char * strDest,const char * strSrc) { if ((NULL==strDest) || (NULL==strSr ...
- C/C++中char* p = "hello" 和 const char* p = "hello"的区别
在写代码常常都会写char * p ="hello";这样的代码,虽然不是错误,但却不建议这样用.应该加const修饰.这句话背后的内涵是什么?下面就刨根问底一下:) 这个行为在不 ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
- 【C++】int、const char*、char*、char、string之间的转换
#include "stdafx.h" #include<string> #include<vector> #include<iostream> ...
- C语言执行时报错“表达式必须是可修改的左值,无法从“const char [3]”转换为“char [120]” ”,原因:字符串不能直接赋值
解决该问题的方法:使用strcpy函数进行字符串拷贝 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string ...
随机推荐
- shell的for循环
与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...
- HackerRank# Candies
原题地址 LeetCode上也有这道题,直接扫一遍就行了,连数组都不用开,感觉像是蕴含了某种动归的思想在里面,要不怎么是个动归题呢 代码: #include <cmath> #includ ...
- BZOJ 1036: [ZJOI2008]树的统计Count 【树链剖分】
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- Java 线程池的原理与实现学习(二)
java类库中提供的线程池简介: java提供的线程池更加强大,相信理解线程池的工作原理,看类库中的线程池就不会感到陌生了. execute(Runnable command):履行Ruannable ...
- robotframework安装和配置【转IBM:https://www.ibm.com/developerworks/cn/opensource/os-cn-robot-framework/index.html】
内容 概览 Robot Framework 介绍 Robot Framework 的安装和配置 RIDE 编辑器介绍 创建测试项目 简单的测试用例的编写 总结 相关主题 评论 Robot Fr ...
- Hubtown
Hubtown 时间限制: 10 Sec 内存限制: 256 MB 题目描述 Hubtown is a large Nordic city which is home to n citizens. ...
- BZOJ1902: Zju2116 Christopher
$n \leq 10^{100}$,问$C_n^m,0<=m<=n$有多少是质数$p \leq 1e7$的倍数. 一样,套高精度的题,只有战胜他才能鄙视他. 但是我TM被他鄙视了一上午!! ...
- Yii 之视图数据块
控制器代码: public $layout = 'common'; public function actionStudent(){ $data = array('page_name'=>'St ...
- hdu2448 / 费用流 / harbin赛区c题
题(自)目(己)错(英)综(语)复(太)杂(差),关系理了半小时+翻译才看明白,看明白之后,直接建图,费用流击杀./简单题. 2A:有的地方,可用互通的要建双向边! #include<cstdi ...
- 安装破解版的webstorne
参考以下链接:https://www.cnblogs.com/cui-cui/p/8507435.html