string 函数库提供了操作字符串的函数,包括:字符串比较函数(strcmp()、strncmp()、strcasecmp())字符串复制函数(strcpy()、strncpy())字符串连接函数(strcat()、strncat())字符串搜索函数(strchr()、strstr())字符串转换函数(strtol()、strtof()、strcpy())字符串格式化函数(sprintf()、sscanf())
C 语言中 string 函数用法
问题:C 语言中 string 函数有什么用?
回答:string 函数库提供了操作字符串的函数,包括字符串比较、复制、连接、搜索、转换和格式化等。
详细说明:
字符串比较函数:
- strcmp():比较两个字符串是否相等
- strncmp():比较两个字符串的前 n 个字符是否相等
- strcasecmp():不区分大小写地比较两个字符串
字符串复制函数:
- strcpy():将一个字符串复制到另一个字符串
- strncpy():将一个字符串的前 n 个字符复制到另一个字符串
字符串连接函数:
- strcat():将一个字符串附加到另一个字符串
- strncat():将一个字符串的前 n 个字符附加到另一个字符串
字符串搜索函数:
- strchr():在字符串中查找第一个指定字符
- strstr():在字符串中查找第一个指定子字符串
字符串转换函数:
- strtol():将字符串转换为 long 整数
- strtof():将字符串转换为 float 浮点数
- strcpy():将一个字符串转换为大写或小写
字符串格式化函数:
- sprintf():将数据格式化为字符串
- sscanf():从字符串中提取数据
示例:
<code class="c">#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 </string.h></stdio.h>