字符串拷贝
strcpy 函数
- 函数描述
- 将 src 的内容拷贝给 dest
- 要保证 dest 的空间足够大 (不安全)
- 函数原型
#include char *strcpy(char *dest, const char *src)
- 返回值
- 函数调用结束,返回值和 dest 参数结果一致
- 返回值
strncpy 函数
- 函数描述
- 将 src 的前 n 个字节拷贝给 dest
- 默认不添加 ‘\0’
- 函数原型
char *strncpy(char *dest, const char *src, size_t n)
- 参数
- n
- 通常 n 与 dest 对应的空间一致
- 如果 n > src,只拷贝 src 的大小;如果 n s2,返回 1,否则返回 -1
- n
- 参数
strncmp 函数
- 函数描述
- 比较 s1 和 s2 两个字符串的前 n 个字符
- 函数原型
int strcmp(const char *s1, const char * s2, size_t n)
- 返回值
- 如果相等,返回 0;如果不等,比较他们对应的 ASCII 码,如果 s1 > s2,返回 1,否则返回 -1
- 返回值
示例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char s1[] = "abcde";
char s2[] = "abcde1";
int a = strcmp(s1, s2);
int b = strncmp(s1, s2, 5);
printf("%d\n", a);
printf("%d\n", b);
system("pause");
return 0;
}
字符串格式化输入、输出
sprintf 函数
- 函数描述
- 对应 printf,将原来写入屏幕的格式化字符串,写到 str 中
- 函数原型
#include int sprintf(char *str, const char *format, ...)
sscanf 函数
- 函数描述
- 对应 scanf,将原来从屏幕获取的格式化字符串,从 str 中获取
- 函数原型
#include int sscanf(const char *str, const char *format, ...)
示例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char str[100];
int a, b, c;
sprintf(str ,"%d + %d = %d", 10, 20, 10+20);
printf("%s\n", str);
sscanf(str, "%d + %d = %d", &a, &b, &c);
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
system("pause");
return 0;
}
字符串查找字符/子串
strchr/strrchr 函数
- 函数描述
- 在字符串 str 中找出一个字符出现的位置,返回字符在字符串中的地址
- 函数原型
char *strchr(const char *s, int c); // 自左向右 char *strrchr(const char *s, int c); // 自右向左
strstr 函数
- 函数描述
- 在字符串 str 中,查找子串 substr 第一次出现的位置,返回地址
- 函数原型
char *strstr(const char *str, const char *substr)
示例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char str[] = "hello world";
char ch = 'o';
char substr[] = "llo";
printf("%s\n", strchr(str, ch)); // o world
printf("%s\n", strrchr(str, ch)); // orld
printf("%s\n", strstr(str, substr)); // llo world
system("pause");
return 0;
}
字符串分割
strtok 函数
- 函数描述
- 按照既定的分隔符,拆分字符串
- 函数原型
char *strtok(char *str, const char *delim)
- 参数
- str
- 待拆分的字符串
- delim
- 分隔符/分隔串
- 其实就是用 ‘\0’ 替换了分隔符
- str
- 返回值
- 字符串拆分后的首地址
- 参数
- 固定用法
- 第一次拆分,str 传拆分的原串
- 第 1+ 次拆分,str 传 NULL
示例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char str[] = "www.baidu.com cnform";
char* p = strtok(str, ". "); // 以 . 空格拆分
while (p != NULL)
{
printf("%s\n", p);
p = strtok(NULL, ". ");
}
printf("%s\n", p);
system("pause");
return 0;
}
/*
www
baidu
com
cn
form
(null)
*/
字符串转换整数
常用于带参数的 main 函数,因为 main 函数的原型是
int main(int argc, char* argv[]);
atoi/atof/atol 函数
- 函数描述
- 字符串转整数/浮点数/长整型
-
原串必须是可转换的字符串
// 错误使用 "abc123" -> 0 "12abc343" -> 12 "123xyc" -> 123
- 函数原型
int atoi(const char *nptr)
示例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char str1[] = "abc123";
char str2[] = "123abc";
char str3[] = "123456";
printf("%d\n", atoi(str1));
printf("%d\n", atoi(str2));
printf("%d\n", atoi(str3));
system("pause");
return 0;
}
/*
0
123
123456
*/