C语言常用字符串处理函数

头文件:#include <string.h>

strlen

size_t strlen( char *str );
功能:函数返回一个整数值,表示给定字符串的长度(不包括结束符'\0')

strcat

char *strcat( char *str1, const char *str2 );
功能:函数将字符串str2 连接到str1的末端,并返回指针str1.

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello";
    char str2[] = " World!";
    strcat(str1, str2);
    printf("%s\n", str1); // 输出: Hello World!
    return 0;
}

strcpy

char *strcpy( char *destc, const char *src );
功能:函数从源字符串src的第一个字符开始,逐个字符地复制到目标字符串dest中,直到遇到源字符串的终止符'\0'。复制完成后,目标字符串dest也以'\0'结尾。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50]; // 确保目标数组足够大以容纳源字符串
    strcpy(dest, src);
    printf("Source: %s\n", src); //输出 Hello, World!
    printf("Destination: %s\n", dest);// Hello, World!
    return 0;
}

现象

strncpy

char *strncpy( char *to, const char *from, size_t count );
功能:将字符串from 中至多count个字符复制到字符串to中。如果字符串from 的长度小于count,其余部分用'\0'填补。返回处理完成的字符串。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50]; // 确保目标数组足够大以容纳源字符串
    strncpy(dest, src,5);
    printf("Source: %s\n", src); //输出 Hello, World!
    printf("Destination: %s\n", dest);
    return 0;
}

现象

strcmp

常用于比较两个字符串是否相等。
int strcmp( const char *str1, const char *str2 );
功能:比较字符串str1 and str2, 返回值如下:
返回值 解释
<0 str1 is less than str2
=0 str1 is equal to str2
>0 str1 is greater than str2

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    int result = strcmp(str1, str2);

    if (result == 0) {
        printf("字符串相等。\n");
    } else if (result < 0) {
        printf("str1 小于 str2。\n");
    } else {
        printf("str1 大于 str2。\n");
    }

    return 0;
}

现象:

比较了两个字符串"Hello"和"World",由于"H"的ASCII值小于"W"的ASCII值,因此strcmp函数返回负数,程序输出"str1 小于 str2"。

strncmp

int strncmp( const char *str1, const char *str2, size_t count );
功能:比较字符串str1 和 str2中至多count个字符。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "HelloABC";
    char str2[] = "HelloDEF";
    int result = strncmp(str1, str2,5);

    if (result == 0) {
        printf("前5个字符相等\n");
    } else if (result < 0 || result >0) {
		printf("前5个字符不相等\n");
	}
    return 0;
}

现象:

strstr

char *strstr( const char *str1, const char *str2 );
功能:函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    const char *str1 = "Hello, World!";
    const char *str2 = "Wo";
    char *result = strstr(str1, str2);

    if (result) {
        printf("Found : %s\n", result);
    } else {
        printf("Not found.\n");
    }

    return 0;
}

现象:

strcspn

常用于定位删除字符串 '\n' 换行符
size_t strcspn( const char *str1, const char *str2 );
功能:函数返回str1 开头连续n个字符都不含字符串str2内字符的字符数。

实例一:

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "!@#$";
    size_t result = strcspn(str1, str2);

    printf("strcspn_result: %zu\n", result);

    return 0;
}

现象:

strcspn函数计算字符串"Hello"中从起始位置开始连续不包含字符串"!@#"中字符的跨度。由于"Hello"这5个字符都不属于"!@#"中的字符,因此result返回5

实例二:

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello\n";
    size_t result = strcspn(str1, "\n");
	str1[result] = '\0';
    printf("strcspn_result: %zu\n", result);
	printf("str1:%s",str1);

    return 0;
}

现象:

atoi

头文件:#include <stdlib.h>
int atoi( const char *str );
功能:将字符串str转换成一个整数并返回结果。参数str 以数字开头,当函数从str 中读到非数字字符则结束转换并将结果返回。

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

int main() {
	int n1,n2;
    const char *str1 = "123abc";
    const char *str2 = "abc123";
    n1 = atoi(str1);
	n2 = atoi(str2);

	printf("n1:%d\r\n",n1);
	printf("n2:%d\r\n",n2);

    return 0;
}

现象:

memset

void *memset( void *buffer, int ch, size_t count );
功能: 函数拷贝ch 到buffer 从头开始的count 个字符里, 并返回buffer指针。 memset() 可以应用在将一段内存初始化为某个值。

cpp 复制代码
#include <stdio.h>
#include <string.h>

#define CLEAR(x)  memset((x),0,sizeof(x))

int main() {
    char str1[] = "Hello,world!";
	printf("str1:%s\r\n",str1);
	
	CLEAR(str1);
	printf("str1:%s\r\n",str1);

    return 0;
}

现象:

相关推荐
lhbweilai17 小时前
分析FreeRTOS内核对象API的源码实现(八)
c语言·开发语言·stm32·单片机
艾络迅Acceleronix18 小时前
环境监测终端:STM32+EG800Z AT+ 艾络迅平台实战|多传感器采集联动全流程
c语言·stm32·单片机·嵌入式硬件·物联网·1024程序员节
Zwawa18 小时前
一只按键不只“按下”:短按、长按和连按如何共存
c语言·单片机·嵌入式
wyhwust21 小时前
程序设计基础--C语言输入输出单元测试
c语言·开发语言
another heaven21 小时前
【C/C++ C++ 面向对象架构的五大特点与能力场景:封装、继承、多态、抽象与 RAII(含实战案例)】
c语言·c++·架构
晓蛋1 天前
什么是C语言
c语言·跨平台·编程语言·高级语言·低级语言
知识分享小能手1 天前
C学习教程,从入门到精通,C语言单片机基础 — 完整知识点手册(19)
c语言·单片机·学习
another heaven1 天前
【C/C++ C++ 新特性中的那些内存优化的特性】
c语言·开发语言·c++
我叫洋洋1 天前
Allegro 画布“垃圾显示“清理指南:把板子调成干净工作台(实战总结)
c语言·c++·单片机·嵌入式硬件·eda·嘉立创
wuminyu1 天前
JVM规范学习总结-Class文件结构
java·linux·c语言·jvm·c++