<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” 源代码总结的更多相关文章

  1. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

  2. 字符串操作函数<string.h>相关函数strcpy,strcat,等源码。

    首先说一下源码到底在哪里找. 我们在文件中包含<cstring>时,如果点击右键打开文档, 会打开cstring,我们会发现路径为: D:\Program Files\visual stu ...

  3. linux下错误的捕获:errno(errno.h)和strerror(string.h)的使用

    参考:http://blog.csdn.net/starstar1992/article/details/52756387 linux下错误的捕获:errno和strerror的使用 经常在调用lin ...

  4. 《C标准库》——之<string.h>

    <string.h>里的字符串操作函数是经常要用到的,因此阅读了源码后自己实现了一些: 拷贝函数 void * Mymemcpy(void * sDst, const void * sSr ...

  5. <string> 与<string.h>、<cstring>的区别

    <string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...

  6. 头文件 string.h cstring string 区别

    1.#include <cstring>   //不可以定义string s:可以用到strcpy等函数using   namespace   std; #include <stri ...

  7. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  8. C/C++关于string.h头文件和string类

    学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...

  9. string.h文件中函数用法

    下面为string.h文件中函数的详细用法: strcpy函数名:strcpy功 能: 拷贝一个字符串到另一个用 法: char *strcpy(char *destin, char *source) ...

随机推荐

  1. sk_buff整理笔记(两、操作函数)

    承接上一:sk_buff 整理笔记(一.数据结构)这一篇要讲的是内核为sk_buff结构提供的一些操作函数. 第一.首先要讲的是sk_buff中的四大指针: 四大指针各自是:head.data.tai ...

  2. Matrix (二维树状数组)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  3. HDU 2647 Reward(图论-拓扑排序)

    Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...

  4. Claris and XOR

    Problem Description Claris loves bitwise operations very much, especially XOR, because it has many b ...

  5. 在四川大学的第二个冠军游戏在线编程:Peter的X

    四川大学线下编程比赛第二题:Peter的X 公布公司: 有 效 期: CSDN 2014-09-27至2015-09-26 难 度 等 级: 答 题 时 长: 编程语言要求: 120分钟 C C++ ...

  6. ABP依赖注入

    ABP依赖注入 点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之6.ABP依赖注入 ABP是“ASP.NET Boilerplate Project (ASP.N ...

  7. bigdata_spark_源码修改_本地环境搭建_eclise

    Eclipse 下开发调试环境的配置该小节中使用的各项工具分别为:mac (Windows 7)+Eclipse Java EE 4.4.2+Scala 2.10.4+Sbt 0.13.8+Maven ...

  8. 第17章 中介者模式(Mediator Pattern)

    原文 第17章 中介者模式(Mediator Pattern) 中介者模式  概述:   在软件开发中,我们有时会碰上许多对象互相联系互相交互的情况,对象之间存在复杂的引用关系,当需求更改时,对系统进 ...

  9. hdu To and Fro

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1200 代码: #include <stdio.h> #include <string ...

  10. 【百度地图API】建立全国银行位置查询系统(五)——如何更改百度地图的信息窗口内容?

    原文:[百度地图API]建立全国银行位置查询系统(五)--如何更改百度地图的信息窗口内容? 摘要: 酷讯.搜房.去哪儿网等大型房产.旅游酒店网站,用的是百度的数据库,却显示了自定义的信息窗口内容,这是 ...