字符串在 C 语言中的操作利器:函数指南205
在 C 语言中,字符串是字符数组,广泛用于存储和处理文本数据。为了方便对字符串进行操作,C 语言提供了一系列内置函数,允许我们高效地执行各种文本操作任务。
字符串长度计算
strlen()
strlen() 函数计算并返回字符串中字符的数量,不包括空字符 '\0'。它用于确定字符串的实际长度。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
int len = strlen(str);
printf("字符串长度:%d", len);
return 0;
}
字符串复制和连接
strcpy()
strcpy() 函数将源字符串复制到目标字符串中,覆盖目标字符串中的现有内容。它返回目标字符串的地址。
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
strcpy(dest, src);
printf("复制后的字符串:%s", dest);
return 0;
}
strcat()
strcat() 函数将源字符串连接到目标字符串的末尾。它修改目标字符串并返回连接后的字符串。
#include <stdio.h>
#include <string.h>
int main() {
char src[] = " World";
char dest[] = "Hello";
strcat(dest, src);
printf("连接后的字符串:%s", dest);
return 0;
}
字符串比较
strcmp()
strcmp() 函数比较两个字符串的词法顺序。它返回一个整数,如果字符串相等则为 0,如果第一个字符串小于第二个字符串则为 -1,如果第一个字符串大于第二个字符串则为 1。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
printf("比较结果:%d", result);
return 0;
}
strncmp()
strncmp() 函数与 strcmp() 类似,但它允许指定要比较的字符数。它将比较给定数量的字符并返回与 strcmp() 相同的结果。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "HelloWorld";
int result = strncmp(str1, str2, 5);
printf("比较前 5 个字符的结果:%d", result);
return 0;
}
字符串搜索
strstr()
strstr() 函数在字符串中搜索子串。如果找到匹配项,它返回子串的地址,否则返回 NULL。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char sub[] = "World";
char *ptr = strstr(str, sub);
if (ptr) printf("子串位置:%d", ptr - str);
else printf("子串未找到");
return 0;
}
strchr()
strchr() 函数在字符串中搜索给定的字符。如果找到匹配项,它返回字符的地址,否则返回 NULL。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char c = 'o';
char *ptr = strchr(str, c);
if (ptr) printf("字符位置:%d", ptr - str);
else printf("字符未找到");
return 0;
}
字符串转换
toupper()
toupper() 函数将字符串中的所有小写字母转换为大写字母。它不修改原始字符串,而是返回转换后的字符串的副本。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char *ptr = toupper(str);
printf("转换为大写:%s", ptr);
return 0;
}
tolower()
tolower() 函数将字符串中的所有大写字母转换为小写字母。它不修改原始字符串,而是返回转换后的字符串的副本。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "HELLO WORLD";
char *ptr = tolower(str);
printf("转换为小写:%s", ptr);
return 0;
}
其他实用函数
memset()
memset() 函数将一段内存设置为指定值。它用于初始化或清除字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
memset(str, '\0', 100);
printf("初始化字符串:%s", str);
return 0;
}
memcpy()
memcpy() 函数将一段内存复制到另一段内存中。它常用于复制字符串。
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[100];
memcpy(dest, src, strlen(src) + 1);
printf("复制字符串:%s", dest);
return 0;
}
atoi()
atoi() 函数将字符串转换为整数。它用于将字符串中的数字提取为整数值。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "1234";
int num = atoi(str);
printf("字符串转换为整数:%d", num);
return 0;
}
printf() 和 scanf()
printf() 和 scanf() 函数可用于格式化地打印和读取字符串。它们是 I/O 操作的通用函数。
#include <stdio.h>
#include <string.h>
int main() {
char input[100];
printf("输入字符串:");
scanf(" %s", input);
printf("输入的字符串:%s", input);
return 0;
}
C 语言中的字符串函数是处理文本数据时的宝贵工具。它们提供了一系列操作,从比较和搜索到转换和格式化。通过熟练掌握这些函数,程序员可以高效地执行各种字符串操作任务,增强代码的可读性和维护性。
2025-02-11
上一篇:C 语言输出详解
Java数组元素:从基础到高级操作的深度解析
https://www.shuihudhg.cn/134539.html
PHP Web应用的安全基石:全面解析数据库SQL注入防御
https://www.shuihudhg.cn/134538.html
Python函数入门到进阶:用简洁代码构建高效程序
https://www.shuihudhg.cn/134537.html
PHP中解析与提取代码注释:DocBlock、反射与AST深度探索
https://www.shuihudhg.cn/134536.html
Python深度解析与高效处理.dat文件:从文本到二进制的实战指南
https://www.shuihudhg.cn/134535.html
热门文章
C 语言中实现正序输出
https://www.shuihudhg.cn/2788.html
c语言选择排序算法详解
https://www.shuihudhg.cn/45804.html
C 语言函数:定义与声明
https://www.shuihudhg.cn/5703.html
C语言中的开方函数:sqrt()
https://www.shuihudhg.cn/347.html
C 语言中字符串输出的全面指南
https://www.shuihudhg.cn/4366.html