string 函数库提供了操作字符串的函数,包括:字符串比较函数(strcmp()、strncmp()、strcasecmp())字符串复制函数(strcpy()、strncpy())字符串连接函数(strcat()、strncat())字符串搜索函数(strchr()、strstr())字符串转换函数(strtol()、strtof()、strcpy())字符串格式化函数(sprintf()、sscanf())

C 语言中 string 函数用法
问题:C 语言中 string 函数有什么用?
回答:string 函数库提供了操作字符串的函数,包括字符串比较、复制、连接、搜索、转换和格式化等。
详细说明:
立即学习“C语言免费学习笔记(深入)”;
字符串比较函数:
- strcmp():比较两个字符串是否相等
- strncmp():比较两个字符串的前 n 个字符是否相等
- strcasecmp():不区分大小写地比较两个字符串
字符串复制函数:
Topsky 是一款针对中小型酒店设计的管理系统,基于 .Net Framework 4.5.2 设计,C# 语言编写,采用 SQL Server 2008 R2 数据库作为数据支持。
- strcpy():将一个字符串复制到另一个字符串
- strncpy():将一个字符串的前 n 个字符复制到另一个字符串
字符串连接函数:
- strcat():将一个字符串附加到另一个字符串
- strncat():将一个字符串的前 n 个字符附加到另一个字符串
字符串搜索函数:
- strchr():在字符串中查找第一个指定字符
- strstr():在字符串中查找第一个指定子字符串
字符串转换函数:
- strtol():将字符串转换为 long 整数
- strtof():将字符串转换为 float 浮点数
- strcpy():将一个字符串转换为大写或小写
字符串格式化函数:
- sprintf():将数据格式化为字符串
- sscanf():从字符串中提取数据
示例:
#include#include 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"); } // 复制字符串 strcpy(str1, str2); printf("str1 现在是 %s\n", str1); // 连接字符串 strcat(str1, "C"); printf("str1 现在是 %s\n", str1); // 搜索字符串 char *pos = strchr(str1, 'o'); if (pos != NULL) { printf("字符 'o' 在字符串中\n"); } // 转换字符串 int num = strtol(str2, NULL, 10); printf("str2 转换为整数为 %d\n", num); return 0; }










