c语言---------字符串操作函数

字符串操作函数 一般以 str 开头 默认遇到'\0' 结束.

需要的头文件#include <string.h>

1、测试字符串长度 strlen

     遇到\0 结束 不包含\0

#include <stdio.h> 
#include <string.h>
void test01()
{
    char *str = "hello world";
    printf("%ld\n", strlen(str));
 }
// 11

2、字符串拷贝 strcpy strncpy

2.1 strcpy char *strcpy(char *dest, const char *src);

功   能:  将 src 指向的空间中的字符串 拷贝到 dst 指向的空间中 遇到\0 结束

返回值:返回的是 dest 保存的空间起始地址编号

void test02()

{

        char *src = "hello world";

        char dst[128] = "";

        strcpy(dst, src);

        printf("%s\n", dst);

}//dst=hello world

2.2 strncpy 拷贝前 n 个字符(拷贝 n 个字节 不足补 0)

char *strncpy(char *dest, const char *src, size_t n);

void test02()

{

        char *src = "hello world";

        char dst[128] = "";

        strncpy(dst, src, 5);

        printf("%s\n", dst);

}//dst=hello

3、字符串追加函数 strcat strncat

char *strcat(char *dest, const char *src);

char *strncat(char *dest, const char *src, size_t n) 

void test03()

{

        char dst[128] = "hello\Oworld";

        char *src = "xixi haha";;strcat(dst, src);

        printf("dst=%s\n", dst);

}//dst=helloxixi haha

4、strcmp 和 strncmp 字符串比较 

strcmp 和 strncmp 是 C 语言中用于比较字符串的函数。

  1. strcmp: 这个函数用于比较两个字符串。如果两个字符串相同,它会返回 0。如果第一个字符串在字典中位于第二个字符串之前,它会返回一个负数。如果第一个字符串在字典中位于第二个字符串之后,它会返回一个正数。

函数原型是:int strcmp(const char *s1, const char *s2);

#include <string.h>

#include <stdio.h>

int main()

{

        char str1[15];

        char str2[15];

        int ret;

        strcpy(str1, "abcdef");

        strcpy(str2, "ABCDEF");

        ret = strcmp(str1, str2);

        if(ret < 0)

        {

                printf("str1 is less than str2");

        }

        else if(ret > 0)

        {

                printf("str1 is greater than str2");

        }

        else

        {

                printf("str1 is equal to str2");

        }

        return(0);

}

在这个例子中,输出将是 "str1 is less than str2",因为字典中的 "abcdef" 在 "ABCDEF" 之前。

2. strncmp: 这个函数比较两个字符串的前 n 个字符。它返回的值和 strcmp 相同。如果两个字符串的前 n 个字符都相同,它返回 0。如果第一个字符串在字典中位于第二个字符串之前,它会返回一个负数。如果第一个字符串在字典中位于第二个字符串之后,它会返回一个正数。如果 n 大于两个字符串的长度,那么函数将只比较到长度较小的字符串。

函数原型是:int strncmp(const char *s1, const char *s2, size_t n);

#include <string.h>

#include <stdio.h>

int main()

{

        char str1[15];

        char str2[15];

        int ret;

        int n = 4; // we want to compare first 4 characters.

        strcpy(str1, "abcdef");

        strcpy(str2, "ABCD");

        ret = strncmp(str1, str2, n);

        if(ret < 0)

        {

                printf("str1 is less than str2");

        }

        else if(ret > 0)

        {

                printf("str1 is greater than str2");

        }

        else

        {

                printf("str1 is equal to str2");

        }

        return(0);

在这个例子中,输出将是 "str1 is greater than str2",因为字典中的 "abcd" 在 "ABCD" 之前。

需要注意的是,strcmp 和 strncmp 只是比较字符串,它们并不检查字符串的边界。因此,如果比较的字符串包含 null 字符('\0'),它们可能会产生不可预知的结果。另外,如果你要比较的字符串很大,或者你要比较的字符串很多,那么这些函数可能会消耗大量的内存和时间。在这种情况下,你可能会希望使用其他的比较方法,比如使用循环逐个比较字符。

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值