5. 常见C语言字符串库函数的使用及实现

1. strncat 函数:

  【函数原型】#include <string.h>

        char *strncat( char *str1, const char *str2, size_t count );

  【功能】将字符串str2 中至多count个字符连接到字符串str1中,追加空值结束符。返回处理完成的字符串。

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main()
{
    char str1[30] = "hello world";
    char str2[20] = "1234.567";

    strncat(str1, str2, 4);          //从str2中拷贝4个字节到str1中

    printf("%s\n", str1);            //hello world1234
    system("pause");
    return ;
}


  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void mystrncat(char *str1, const char *str2, int count)
{
    if (str1 == NULL || str2 == NULL)
        return;

    char *p = str1;
    while (*p != '\0')
    {
        p++;
    }                                //p指向了str1的末端'\0'
    for (int i = 0; i < count; i++)  //前进count个字符
    {
        *p = str2[i];                //赋值
        p++;                         //指针前进
    }
    *p = '\0';
}

void main()
{
    char str1[30] = "hello world";
    char str2[20] = "1234.567";

    //strncat(str1, str2, 4);        //从str2中拷贝4个字节到str1中
    mystrncat(str1, str2, 4);        //从str2中拷贝4个字节到str1中

    printf("%s\n", str1);            //hello world1234
    system("pause");
    return ;
}

2. atoi 函数:

  【函数原型】#include <stdlib.h>

        int atoi( const char *str );

  【功能】将字符串str转换成一个整数并返回结果。参数str 以数字开头,当函数从str 中读到非数字字符则结束转换并将结果返回。例如,

      i = atoi( "512.035" );

      i 的值为 512

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>

void main()
{
    //char str[10] = "8848";
    //int num = atoi(str);
    //printf("%d\n", num);        //8848

    //char str[10] = "8848";
    //int num = atoi(str+1);
    //printf("%d\n", num);        //848

    //转换的时候,传递字符串的首地址,但地址不要求是首地址
    //字符串的任何地址都可以,num起到接收赋值的作用
    //转换成功是整数,失败是0,出现非数字字符都会转换失败
    char str[10] = "e8848";
    int num = atoi(str);
    printf("%d\n", num);          //0
    
    system("pause");
    return ;
}

  【库函数实现】

3. strrev 函数:

  【函数原型】#include <string.h>

        void strrev( char *str);

  【功能】将字符串str逆转。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str[20] = "hello8848";
    printf("原来字符串:%s\n", str);    //hello8848

    _strrev(str);        //_strrev与strrev用法完全一样

    printf("后来字符串:%s\n", str);    //8488olleh

    system("pause");
    return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void mystrrev(char *str)
{
    int len = strlen(str);                //获取字符串长度
    for (int i = 0; i < (len / 2); i++)   //循环一半,交换字符
    {
        char ch = str[i];                 //对调字符
        str[i] = str[len - 1 - i];        //数组a[i]最大的元素是a[i-1]
        str[len - 1 - i] = ch;
    }
}

void main()
{
    char str[20] = "hello8848";
    printf("原来字符串:%s\n", str);    //hello8848

    //_strrev(str);        //_strrev与strrev用法完全一样
    mystrrev(str);

    printf("后来字符串:%s\n", str);    //8488olleh

    system("pause");
    return ;
}

4. strupr 函数 与 strlwr 函数:

  【函数原型】void strupr(char *str);

        void strlwr(char *str);

  【功能】strupr()是将字符串小写转为大写;strlwr()是将字符串大写转为小写。

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str[50] = "notepad";

    _strupr(str);        //小写转大写
    printf("%s\n", str);            //NOTEPAD

    _strlwr(str);        //大写转小写
    printf("%s\n", str);            //notepad
    
    system("pause");
    return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void toBig(char *str)
{
    while (*str != '\0')         //判断字符串是否结束
    {
        if ((*str) >= 'a' && (*str) <= 'z')    //判断是否小写字母
        {
            *str = *str - 32;    //小写转大写 A--65 a--97

        }
        str++;                    //继续循环
    }
}

void toSmall(char *str)
{
    while (*str != '\0')         //判断字符串是否结束
    {
        if ((*str) >= 'A' && (*str) <= 'Z')    //判断是否大写字母
        {
            *str = *str + 32;    //大写转小写 A--65 a--97

        }
        str++;                    //继续循环
    }
}

void main()
{
    char str[50] = "notepad";

    //_strupr(str);        //小写转大写
    toBig(str);            //小写转大写
    printf("%s\n", str);            //NOTEPAD

    //_strlwr(str);        //大写转小写
    toSmall(str);          //大写转小写
    printf("%s\n", str);            //notepad
    
    system("pause");
    return ;
}

5. strlen 函数:

  【函数原型】#include <string.h>

        size_t strlen( char *str );

  【功能】函数返回字符串str 的长度( 即空值结束符之前字符数目)。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str[100] = "I love iphone";         //str是变量
    char *p = "I love china";                //p是常量

    int len1 = strlen(str);
    int len2 = strlen(p);

    printf("str=%d,p=%d\n", len1, len2);    //str=13,p=12
    
    system("pause");
    return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mystrlen(char *str)
{
    int len = 0;              //长度
    while (*str != '\0')      //检测字符串是否结束
    {
        len++;                //继续计数
        str++;                //指针继续前进
    }
    return len;
}

void main()
{
    char str[100] = "I love iphone";         //str是变量
    char *p = "I love china";                //p是常量

    //int len1 = strlen(str);
    //int len2 = strlen(p);

    int len1 = mystrlen(str);
    int len2 = mystrlen(p);

    printf("str=%d,p=%d\n", len1, len2);     //str=13,p=12
    
    system("pause");
    return ;
}

6. strcpy 函数 与 strcat 函数:实现两个字符串连接,放到第三个字符串中

  【函数原型】#include <string.h>

        char *strcpy( char *to, const char *from );

        char *strcat( char *str1, const char *str2 );

  【功能】strcpy:复制字符串from 中的字符到字符串to,包括空值结束符。返回值为指针to

      strcat :函数将字符串str2 连接到str1的末端,并返回指针str1.

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str1[10] = "note";
    char str2[10] = "pad";
    char str[20];

    //方法一:使用sprintf字符串打印函数
    //sprintf(str, "%s%s", "note", "pad");
    //printf("%s\n", str);                  //notepad

    //方法二:库函数strcpy和strcat
    strcpy(str, str1);        //字符串拷贝
    printf("%s\n", str);                    //note
    strcat(str, str2);        //字符串连接
    printf("%s\n", str);                    //notepad

    system("pause");
    return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void * mystrcpy(char *to, char *from)
{
    char *p = to;            //备份首地址
    if (to == NULL || from == NULL)
        return NULL;         //字符串为空,不需要拷贝

    while (*from != '\0')
    {
        *to = *from;         //一个字符一个字符的拷贝
        to++;
        from++;
    }
    *to = '\0';
    return p;
}

void * mystrcat(char *strDest, char *strSrc)
{
    char *p = strDest;          //备份首地址
    if (strDest == NULL || strSrc == NULL)
        return NULL;

    while (*strDest != '\0')    //循环到strDest末尾'\0'
    {
        strDest++;
    }
    while (*strSrc != '\0')
    {
        *strDest = *strSrc;
        strDest++;
        strSrc++;
    }
    *strDest = '\0';
    return p;
}

void main()
{
    char str1[10] = "note";
    char str2[10] = "pad";
    char str[20];

    //方法一:使用sprintf字符串打印函数
    //sprintf(str, "%s%s", "note", "pad");
    //printf("%s\n", str);                  //notepad

    //方法二:库函数strcpy和strcat
    //strcpy(str, str1);        //字符串拷贝
    mystrcpy(str, str1);        //字符串拷贝
    printf("%s\n", str);                    //note
    //strcat(str, str2);        //字符串连接
    mystrcat(str, str2);        //字符串连接
    printf("%s\n", str);                    //notepad

    system("pause");
    return ;
}

7. strchr 函数:

  【函数原型】#include <string.h>

        char *strchr( const char *str, int ch );

  【功能】函数返回一个指向strch 首次出现的位置,当没有在str 中找ch到返回NULL。

  【库函数使用】

#include <stdio.h>
#include <string.h>

void main()
{
    char str[20] = "I love iphone";
    char ch = 'v';

    char *p = strchr(str, ch);

    if (p == NULL)
        printf("没有找到\n");
    else
        printf("值%c,地址%x.\n", *p, p);
    
    system("pause");
    return ;
}

  【库函数实现】

#include <stdio.h>
#include <string.h>

char *mystrchr(char *str, char ch)
{
    if (str == NULL)
        return NULL;

    else
    {
        while (*str != '\0')
        {
            if (*str == ch)       //相等,跳出循环
                return str;
            str++;                //指针向前移动
        }
    }

    return NULL;
}

void main()
{
    //char str[20] = "I love iphone";
    //char ch = 'v';
    //char *p = strchr(str, ch);

    char str[20] = "I love iphone";
    char ch = 'a';
    char *p = mystrchr(str, ch);

    if (p == NULL)
        printf("没有找到\n");
    else
        printf("值%c,地址%x.\n", *p, p);
    
    system("pause");
    return ;
}

8. strcmp 函数:

  【函数原型】#include <string.h>

        int strcmp( const char *str1, const char *str2 );

  【功能】比较字符串str1 and str2, 返回值为0表示相等,返回不为0表示不等。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str1[10] = "note";
    char str2[10] = "note";

    if (strcmp(str1, str2) == 0)
        printf("相等\n");            //相等
    else
        printf("不等\n");
    
    system("pause");
    return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mystrcmp(char *str1, char *str2)
{
    int len1 = strlen(str1);
    int len2 = strlen(str2);

    if (len1 != len2)
        return -1;
    else
    {
        int flag = 1;        //假定相等
        for (int i = 0; i < len1; i++)
        {
            if (str1[i] != str2[i])        //出现一个字符不等
            {
                flag = 0;
                break;
            }
            if (flag == 1)
                return 0;
            else
                return -1;
        }
    }
}

void main()
{
    char str1[10] = "note";
    char str2[10] = "note";

    //if (strcmp(str1, str2) == 0)
    //    printf("相等\n");            //相等
    //else
    //    printf("不等\n");

    if (mystrcmp(str1, str2) == 0)
        printf("相等\n");            //相等
    else
        printf("不等\n");
    
    system("pause");
    return ;
}

9. strstr 函数:

  【函数原型】#include <string.h>

         char *strstr( const char *str1, const char *str2 );

  【功能】函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str1[40] = "I love iphone I love app";
    char str2[10] = "iphone";

    char *p = strstr(str1, str2);            //检索子串

    if (p == NULL)
        printf("没有找到\n");
    else
        printf("值%c,地址%x\n", *p, p);      //值i,地址...

    system("pause");
    return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *mystrstr(char *str1, char *str2)
{
    if (str1 == NULL || str2 == NULL)
        return NULL;

    int len1 = strlen(str1);    //母串的长度
    int len2 = strlen(str2);    //子串的长度

    for (int i = 0; i < len1 - len2; i++)
    {
        int flag = 1;                    //标记,假定字符串一开始相等

        for (int j = 0; j < len2; j++)
        {
            if (str1[i + j] != str2[j])    //有一个字符不等
            {
                flag = 0;
                break;                    //有一个不等跳出循环
            }
        }
        if (flag == 1)
        {
            return (str1 + i);            //返回找到的地址
            break;
        }
    }
    return NULL;
}

void main()
{
    char str1[40] = "I love iphone I love app";
    char str2[10] = "iphone";

    //char *p = strstr(str1, str2);            //检索子串
    char *p = mystrstr(str1, str2);            //检索子串

    if (p == NULL)
        printf("没有找到\n");
    else
        printf("值%c,地址%x\n", *p, p);    //值i,地址...

    system("pause");
    return ;
}

10. strset 函数:

  【函数原型】#include <string.h>

         char *strset(char *str, char ch );

  【功能】将字符串设置为制定字符,常用于字符串清0操作。

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char str[20] = "helloworld8832";
    printf("原来的str:%s\n", str);          //helloworld8832

    _strset(str, '8');

    printf("修改后的str:%s\n", str);        //88888888888888

    system("pause");
    return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void mystrset(char *str, char ch)
{
    while (*str != '\0')      //遍历字符串
    {
        *str = ch;            //赋值字符
        str++;                //字符串指针不断向前
    }
}

void main()
{
    char str[20] = "helloworld8832";
    printf("原来的str:%s\n", str);          //helloworld8832

    //_strset(str, '8');
    mystrset(str, '8');

    printf("修改后的str:%s\n", str);        //88888888888888

    system("pause");
    return ;
}