“String.h” 源代码总结
<String.h> 总结:
常用的函数:
一、memchr:
说明:当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的指针;否则返回NULL。
代码:
#include <stdio.h>
void *memchr(const void*s, int c, size_t n){
const unsigned char uc = c;
const unsigned char *su;
< n; ++su, --n)
if(*su == uc)
return ((void *)su);
return (NULL);
}
int main(int argc, char const *argv[])
{
char*s ="I am Levi !";
char *p;
p );
if(p) printf("%s\n", p);
else printf("Not Found!\n");
;
}
// find first occurrence if c in s[n].
二、memcmp:
说明:比较内存区域buf1和buf2的前count个字节
代码:
#include <stdio.h>
int memcmp(const void *s1, const void *s2, size_t n){
const unsigned char *su1, *su2;
< n; ++su1, ++su2, --n)
if(*su1 != *su2)
return ((*su1 < *su2) ? -1 : +1);
;
}
int main(int argc, char const *argv[])
{
char*s1 ="Cello kitty1";
char*s2 ="Dello kitty";
int r;
r );
if(!r) printf("s1 and s2 are identical\n");
) printf("s1 less than s2\n");
else printf("s1 greater than s2\n");
;
}
//compare unsigned char (s1[n] and s2[n]).
三、memcpy
说明:由src所指内存区域复制count个字节到dest所指内存区域
代码:
#include <stdio.h>
void *memcpy(void *s1, const void *s2, size_t n){
char *su1;
const char *su2;
< n; ++su1, ++su2, --n)
*su1 = *su2;
return (s1);
}
int main(int argc, char const *argv[])
{
char*s ="Hello Levi";
];
memcpy(d, s, );
printf("%s\n", d);
puts(d);
;
}
/* copy char s2[n] to s1[n] in any order */
四、memmove
说明:
1、由src所指内存区域复制count个字节到dest所指内存区域。
2、src和dest所指内存区域可以重叠,但复制后src内容会被更改。函数返回指向dest的指针。
代码:
#include <stdio.h>
void *memmove(void *s1, const void *s2, size_t n){
char *sc1;
const char *sc2;
sc1 = s1;
sc2 = s2;
if(sc2 > sc1 && sc1 < sc2 + n)
< n; --n)
*--sc1 = *--sc2;
else
< n; --n)
*sc1++ = *sc2++;
return (s1);
}
int main(int argc, char const *argv[])
{
char s[] ="wushirenfei shitaiyanliang";
memmove(s, s);
printf("%s\n", s);
;
}
/* copy char s2[n] to s1[n] safely */
五、memset
说明:
1、把buffer所指内存区域的前count个字节设置成字符c。
2、返回指向buffer的指针。
代码:
#include <stdio.h>
void *memset(void *s, int c, size_t n){
const unsigned char uc = c;
unsigned char *su;
< n; ++su, --n)
*su = uc;
return (s);
}
int main(int argc, char const *argv[])
{
] ="Bu xiang yan hua lu shang xing";
memset(s);
printf("%s\n", s);
;
}
六、strncat、strcat
说明:
1、strcat 把src所指字符串添加到dest结尾处并添加'\0'。
2、src、dest 不可以重叠
代码:
#include <stdio.h>
char *strncat(char *s1, const char *s2, size_t n){
char *s;
for(s = s1; *s != '\0'; ++s)
;
< n && *s2 != '\0'; --n)
*s++ = *s2++;
*s = '\0';
return (s1);
}
char *strcat(char *s1, const char *s2){
char *s;
for(s = s1; *s != '\0'; ++s)
;
for(; (*s = *s2) != '\0'; ++s, ++s2)
;
return s1;
}
int main()
{
]="Golden Global";
char *s=" View WinIDE Library";
strcat(d,s);
printf("%s\n",d);
;
}
七、strcpy、strncpy
说明:
1、把src所指由NULL结束的字符串复制到dest所指的数组中
2、src和dest所指内存区域不可以重叠
代码:
#include <stdio.h>
char *strcpy(char *s1, const char *s2){
char *s;
for(s = s1; (*s++ = *s2++) != '\0'; )
;
return (s1);
}
char *strncpy(char *s1, const char *s2, size_t n){
char *s;
< n && *s2 != '\0'; --n)
*s++ = *s2++;
< n; --n)
*s++ = '\0';
return s1;
}
int main(int argc, char const *argv[])
{
char*s ="Levi is Hacker";
];
strcpy(d, s);
printf("%s\n", d);
;
}
八、strcmp、strncmp
说明:
1、比较字符串s1和s2
2、如何比较,看代码吧
代码:
#include <stdio.h>
int strncmp(const char *s1, const char *s2, size_t n){
< n; ++s1, ++s2, --n)
if(*s1 != *s2)
return((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
else if(*s1 == '\0')
;
;
}
int strcmp(const char *s1, const char *s2){
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == '\0')
;
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
}
int main(int argc, char const *argv[])
{
char *s1="Ze";
char *s2="Hel";
int r;
r = strcmp(s1, s2);
if(!r)
printf("s1 and s2 are identical\n");
else
if(r<0)
printf("s1 less than s2\n");
else
printf("s1 greater than s2\n");
;
}
九、strlen
说明:返回字符串长度
代码:
#include <stdio.h>
size_t strlen(const char*s){
const char *sc;
for(sc = s; *sc != '\0'; ++sc)
;
return sc - s;
}
int main(int argc, char const *argv[])
{
int n;
char *s ="Hello Levi";
n = strlen(s);
printf("%d\n", n);
;
}
十、strchr
说明:
1、查找s中首次出现字符c位置
2、返回指针,指向c出现位置,否则返回NULL
代码:
#include <stdio.h>
char *strchr(const char *s, int c){
const char ch = c;
for(; *s != ch; ++s)
if(*s == '\0')
return NULL;
return (char *)s;
}
int main(int argc, char const *argv[])
{
char *s = "Golden Global View";
char *p;
p = strchr(s,'i');
if(p)
printf("%s\n", p);
else
printf("Not Found!\n");
;
}
十一、strcspn
说明:
1、在字符串s1中搜索s2出现的字符。
2、返回找到字符的下标值
代码:
#include <stdio.h>
size_t strcspn(const char *s1, const char *s2){
const char *sc1, *sc2;
for(sc1 = s1; *sc1 != '\0'; ++sc1)
for(sc2 = s2; *sc2 != '\0'; ++sc2)
if(*sc1 == *sc2)
return (sc1 - s1);
return (sc1 - s1);
}
int main(int argc, char const *argv[])
{
char *s="Golden Global View";
char *r="L";
int n = strcspn(s,r);
printf("%d\n", n);
;
}
十二、strrchr
说明:
1、在字符串搜索c字符
2、返回指针,指向搜索c字符最后出现的字符下标
代码:
#include <stdio.h>
char *strrchr(const char *s, int c){
const char ch = c;
const char *sc;
for(sc = NULL; ; ++s){
if(*s == ch)
sc = s;
if(*s == '\0')
return ((char *)sc);
}
}
int main(int argc, char const *argv[])
{
char *s = "Hello LLLLevi";
char *p = strrchr(s, 'L');
printf("%s\n", p);
;
}
十三、strstr
说明:
1、返回父串完全匹配子串的指针
2、返回的指针指向完全匹配子串的position
代码:
#include <stdio.h>
char *strchr(const char *s, int c){
const char ch = c;
for(; *s != ch; ++s)
if(*s == '\0')
return NULL;
return (char *)s;
}
char *strstr(const char *s1, const char *s2){
if(*s2 == '\0')
return ((char *)s1);
for(; (s1 = strchr(s1, *s2)) != NULL; ++s1){
const char *sc1, *sc2;
for(sc1 = s1, sc2 = s2; ; )
if(*++sc2 == '\0')
return ((char *)s1);
else if(*++sc1 != *sc2)
break;
}
return NULL;
}
int main(int argc, char const *argv[])
{
char *s = "ccdd View";
char *l = "dd";
char *p;
p=strstr(s,l);
if(p)
printf("%s\n", p);
else
printf("Not Found!");
;
}
Ps:深圳冬天还有蚊子,擦擦,蚊子太多了,不写了,回宿舍睡觉觉~~
“String.h” 源代码总结的更多相关文章
- C string.h 常用函数
参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...
- 字符串操作函数<string.h>相关函数strcpy,strcat,等源码。
首先说一下源码到底在哪里找. 我们在文件中包含<cstring>时,如果点击右键打开文档, 会打开cstring,我们会发现路径为: D:\Program Files\visual stu ...
- linux下错误的捕获:errno(errno.h)和strerror(string.h)的使用
参考:http://blog.csdn.net/starstar1992/article/details/52756387 linux下错误的捕获:errno和strerror的使用 经常在调用lin ...
- 《C标准库》——之<string.h>
<string.h>里的字符串操作函数是经常要用到的,因此阅读了源码后自己实现了一些: 拷贝函数 void * Mymemcpy(void * sDst, const void * sSr ...
- <string> 与<string.h>、<cstring>的区别
<string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...
- 头文件 string.h cstring string 区别
1.#include <cstring> //不可以定义string s:可以用到strcpy等函数using namespace std; #include <stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C/C++关于string.h头文件和string类
学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...
- string.h文件中函数用法
下面为string.h文件中函数的详细用法: strcpy函数名:strcpy功 能: 拷贝一个字符串到另一个用 法: char *strcpy(char *destin, char *source) ...
随机推荐
- windows server 2008见安装IIS方法(解决)
windows server 2008见安装IIS方法(解决) 刚开始有点蒙,后来才知道原来如此.! . 右键点击[我的电脑]--[管理]--[字符]--[加入角色]--仅落后win7像.啊! 版权声 ...
- 网络资源(3) - iBatis视频
2018_08_24 http://v.youku.com/v_show/id_XMjk2ODY2OTE2.html iBatis视频教程01
- 如何定义自己的ViewGroup
在发展中,有时会遇到一些要求.布局和控制系统不仅提供使用,以满足我们的发展,所以这一次就行,通常是你自己的自定义布局(ViewGroup)并控制(View)该.我在这里,我们将用一个简单的例子,当他们 ...
- awk学习总结(两) How awk works and awk CMD in a file
测试文件names Tom Savage 100 Molly Lee 200 John Doe 300 $0 代表file的整行; $1,第一列;$2,第二列...... $ awk '/Tom/{p ...
- CQRS
CQRS 2015-06-04 15:33 by 敏捷的水, 177 阅读, 0 评论, 收藏, 编辑 CQRS是Command Query Responsibility Seperation(命令查 ...
- Java设计模式(四) 装饰 代理模式
(七)装饰 Decorator 装饰是一个对象,以动态地增加一些新功能. 象与被装饰的对象须要实现同一个接口.装饰对象持有被装饰对象的实例. interface DecoratorSourceable ...
- linux_ubuntu 16.04 更新wifi驱动_无法链接wifi问题
ubuntu kylin ubuntu kylin ubuntu kylin wifi 这个很好解决的,16.04 默认 没有使用wifi驱动设备,默认选择的是:不使用设备1.进入到,软件和更新 -- ...
- IMSDroid遇到注册问题(蘼1S 计3等一下 Android4.4)
最近的研究视频通话,开源项目IMSDroid编译测试,这实在是不幸的,饭1 Android4.1和大米3 Android4.4该系统不是对生命和死亡登记.... .后来通过大神日志分析和建议.发现改变 ...
- 【高德API】如何利用MapKit开发全英文检索的iOS地图
原文:[高德API]如何利用MapKit开发全英文检索的iOS地图 制作全英文地图的展示并不困难,但是要制作全英文的数据检索列表,全英文的信息窗口,你就没办法了吧.告诉你,我有妙招!使用iOS自带的M ...
- 关于小改CF协同过滤至MapReducer上的一些心得
至上次重写ID3 MR版之后,手贱继续尝试CF.之前耳闻CF这两年内非常火,论内某大神也给了单机版(90%代码来自于其).所以想试试能否改到MR上.整体来说,CF本身的机制以相似性为核心,与迭代调用几 ...