C 语言字符串输出全面指南117
在 C 语言中,字符串是一种由字符序列组成的特殊数据类型。字符串在现实世界应用中非常常见,例如用户输入、文件内容和程序输出。掌握字符串输出技术对于 C 程序员至关重要。
打印单个字符
要打印单个字符,可以使用 putchar() 函数。它接受一个字符字符作为参数并将其输出到标准输出设备(通常是控制台)。#include
int main()
{
putchar('A');
return 0;
}
打印字符串
要打印整个字符串,可以使用 printf() 和 fputs() 函数。
使用 printf() 打印字符串
printf() 函数广泛用于格式化输出,包括字符串。它遵循特定语法,其中 %s 占位符用于打印字符串。#include
int main()
{
char str[] = "Hello, world!";
printf("%s", str);
return 0;
}
使用 fputs() 打印字符串
fputs() 函数专门用于向流(如标准输出)写入字符串。它接受一个字符串指针作为参数。#include
int main()
{
char str[] = "Hello, world!";
fputs(str, stdout);
return 0;
}
获取字符串长度
在某些情况下,确定字符串长度可能很有用。C 语言提供了 strlen() 函数来返回字符串的长度。#include
int main()
{
char str[] = "Hello, world!";
int len = strlen(str);
printf("字符串长度:%d", len);
return 0;
}
字符串拼接
字符串拼接涉及将两个或多个字符串组合成一个新字符串。C 语言提供了 strcat() 函数用于此目的。#include
int main()
{
char str1[] = "Hello";
char str2[] = "world";
strcat(str1, ", ");
strcat(str1, str2);
printf("拼接后的字符串:%s", str1);
return 0;
}
字符串比较
比较字符串对于各种操作(如排序和搜索)很有用。C 语言提供了 strcmp() 和 strncmp() 函数用于比较字符串。
使用 strcmp() 比较字符串
strcmp() 函数比较两个字符串并返回一个整数。如果第一个字符串小于第二个字符串,则返回 -1;如果第一个字符串大于第二个字符串,则返回 1;如果两个字符串相等,则返回 0。#include
int main()
{
char str1[] = "Apple";
char str2[] = "Banana";
int result = strcmp(str1, str2);
if (result < 0)
printf("Apple 小于 Banana");
else if (result == 0)
printf("Apple 等于 Banana");
else
printf("Apple 大于 Banana");
return 0;
}
使用 strncmp() 比较字符串(指定长度)
strncmp() 函数类似于 strcmp(),但它比较字符串的前 n 个字符,其中 n 是指定的长度。#include
int main()
{
char str1[] = "Apple Blossom";
char str2[] = "Apple Butter";
int result = strncmp(str1, str2, 6);
if (result == 0)
printf("前 6 个字符相等");
else
printf("前 6 个字符不相等");
return 0;
}
字符串复制
字符串复制涉及将一个字符串的内容复制到另一个字符串中。C 语言提供了 strcpy() 和 strncpy() 函数用于此目的。
使用 strcpy() 复制字符串
strcpy() 函数将源字符串的内容复制到目标字符串中,直到遇到空字符('\0')为止。#include
int main()
{
char src[] = "Hello, world!";
char dest[20];
strcpy(dest, src);
printf("复制后的字符串:%s", dest);
return 0;
}
使用 strncpy() 复制字符串(指定长度)
strncpy() 函数将源字符串的前 n 个字符复制到目标字符串中,其中 n 是指定的长度。#include
int main()
{
char src[] = "Apple Blossom";
char dest[10];
strncpy(dest, src, 6);
dest[6] = '\0'; // 手动添加空字符以终止字符串
printf("复制前 6 个字符:%s", dest);
return 0;
}
字符串查找
在字符串中查找特定字符或子字符串至关重要。C 语言提供了 strchr()、strstr() 和 strrchr() 函数用于此目的。
使用 strchr() 查找字符
strchr() 函数在字符串中查找第一个匹配的字符并返回指向它的指针。如果未找到该字符,则返回 NULL。#include
int main()
{
char str[] = "Hello, world!";
char *result = strchr(str, 'o');
if (result != NULL)
printf("第一个 'o' 的位置:%d", (result - str) + 1);
else
printf("找不到 'o'");
return 0;
}
使用 strstr() 查找子字符串
strstr() 函数在字符串中查找第一个匹配的子字符串并返回指向它的指针。如果未找到该子字符串,则返回 NULL。#include
int main()
{
char str[] = "Hello, world!";
char *result = strstr(str, "world");
if (result != NULL)
printf("子字符串 'world' 的位置:%d", (result - str) + 1);
else
printf("找不到子字符串 'world'");
return 0;
}
使用 strrchr() 查找字符(从尾部开始)
strrchr() 函数在字符串中从尾部开始查找第一个匹配的字符并返回指向它的指针。如果未找到该字符,则返回 NULL。#include
int main()
{
char str[] = "Hello, world!";
char *result = strrchr(str, 'l');
if (result != NULL)
printf("最后一个 'l' 的位置:%d", (result - str) + 1);
else
printf("找不到最后一个 'l'");
return 0;
}
字符串分割
字符串分割涉及将字符串拆分成较小的部分,通常基于特定的分隔符。C 语言提供了 strtok() 函数用于此目的。#include
int main()
{
char str[] = "Hello, world, how are you?";
const char delim[] = ", ";
char *ptr = strtok(str, delim);
while (ptr != NULL) {
printf("分割后的部分:%s", ptr);
ptr = strtok(NULL, delim);
}
return 0;
}
字符大小写转换
C 语言提供了 toupper() 和 tolower() 函数,用于将字符转换为大写和小写。
使用 toupper() 转换为大写
toupper() 函数将给定的小写字符转换为大写字符。#include
#include
int main()
{
char ch = 'a';
ch = toupper(ch);
printf("大写字符:%c", ch);
return 0;
}
使用 tolower() 转换为小写
tolower() 函数将给定的大写字符转换为小写字符。#include
2024-10-25
上一篇:C 语言中字符串的输出
Python字符串查找与判断:从基础到高级的全方位指南
https://www.shuihudhg.cn/134118.html
C语言如何高效输出字符串“inc“?深度解析printf、puts及格式化输出
https://www.shuihudhg.cn/134117.html
PHP高效获取CSV文件行数:从小型文件到海量数据的最佳实践与性能优化
https://www.shuihudhg.cn/134116.html
C语言控制台图形输出:从入门到精通的ASCII艺术实践
https://www.shuihudhg.cn/134115.html
Python在Linux环境下的执行与自动化:从基础到高级实践
https://www.shuihudhg.cn/134114.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