C 语言字符串输入输出371
在 C 语言中,字符串是一种数据类型,用于存储文本。字符串由一个或多个字符组成,以空字符 '\0' 结尾。C 语言提供了内置函数来处理字符串输入和输出。
字符串输入
C 语言中获取字符串输入的常用函数是 scanf。该函数使用格式说明符 %s 来读取字符串,直到遇到空格或换行符。
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("The entered string is: %s", str);
return 0;
}
此代码提示用户输入一个字符串,并将输入存储在 str 数组中。请注意,由于 scanf 不会读取空格或换行符,因此用户需要确保输入的字符串不包含这些字符。
字符串输出
C 语言中字符串输出的常用函数是 printf。该函数使用格式说明符 %s 来打印字符串。
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
printf("%s", str);
return 0;
}
此代码打印字符串 "Hello, world!" 到控制台。请注意,printf 会自动在字符串末尾添加换行符。
字符串长度计算
C 语言中计算字符串长度的常用函数是 strlen。该函数返回字符串中不包括终止空字符 '\0' 的字符数。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
printf("The length of the string is: %d", len);
return 0;
}
此代码计算字符串 "Hello, world!" 的长度,并打印结果 13(不包括终止空字符)。
字符串复制
C 语言中复制字符串的常用函数是 strcpy。该函数将源字符串复制到目标字符串中。
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, world!";
char dest[100];
strcpy(dest, src);
printf("The copied string is: %s", dest);
return 0;
}
此代码将字符串 "Hello, world!" 复制到 dest 数组中。请注意,dest 数组必须有足够的空间来容纳源字符串,包括终止空字符。
字符串比较
C 语言中比较字符串的常用函数是 strcmp。该函数返回以下值之一:* 0,如果字符串相等
* 正数,如果第一个字符串大于第二个字符串
* 负数,如果第一个字符串小于第二个字符串
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "Hello, world!";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.");
} else if (result > 0) {
printf("The first string is greater than the second string.");
} else {
printf("The first string is less than the second string.");
}
return 0;
}
此代码比较字符串 "Hello, world!" 和 "Hello, world!",并打印结果 "The strings are equal."。
字符串拼接
C 语言中连接字符串的常用函数是 strcat。该函数将源字符串附加到目标字符串后面。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, ";
char str2[] = "world!";
strcat(str1, str2);
printf("The concatenated string is: %s", str1);
return 0;
}
此代码将字符串 "Hello, " 和 "world!" 连接起来,并打印结果 "Hello, world!"。
字符串查找
C 语言中查找字符串中子字符串的常用函数是 strstr。该函数返回子字符串在字符串中第一次出现的位置,或 NULL 如果找不到子字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
char substr[] = "world";
char *result = strstr(str, substr);
if (result) {
printf("The substring was found at position %d.", result - str);
} else {
printf("The substring was not found.");
}
return 0;
}
此代码在字符串 "Hello, world!" 中查找子字符串 "world",并打印结果 "The substring was found at position 7."。
字符串格式化
C 语言中格式化字符串的常用函数是 sprintf。该函数将格式化的输出写入一个字符串。
#include <stdio.h>
int main() {
char str[100];
int age = 30;
sprintf(str, "My age is %d.", age);
printf("%s", str);
return 0;
}
此代码将格式化的字符串 "My age is 30." 写入 str 数组,并打印结果。
字符串转换
C 语言中转换字符串到不同数据类型的常用函数包括:* atoi:将字符串转换为整数
* atof:将字符串转换为浮点数
* strtol:将字符串转换为长整数
* strtod:将字符串转换为双精度浮点数
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "30";
int age = atoi(str);
printf("The age is: %d", age);
return 0;
}
此代码将字符串 "30" 转换为整数 30,并打印结果。
2024-10-24
上一篇:函数:C 语言编程的基础
下一篇: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