发布时间:2023-12-19 09:30
1、字符串通常放在常量字符串 中或者 字符数组 中。
2、字符串常量 适用于那些对它不做修改的字符串函数
#include
int main()
{
const char* str1 = "abcdef";
const char* str2 = "bbb";
printf("%d %d\n", strlen(str1), strlen(str2)); // 6 3
return 0;
}
#include
#include
int main()
{
char str1[20];
strcpy(str1, "To be");
puts(str1); // To be
strcpy(str1, "abcdefgh");
puts(str1); // abcdefgh
return 0;
}
#include
#include
int main()
{
char str1[20];
char str2[20];
strcpy(str1, "To be ");
strcpy(str2, "or not to be");
strncat(str1, str2, 6); // 追加6个字符到尾部
puts(str1); // To be or not
puts(str2); // or not to be
return 0;
}
#include
#include
int main()
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
puts("Looking for R2 astromech droids...");
for (n = 0; n < 3; n++)
{
if (strncmp(str[n], "R2xx", 2) == 0) // 比较前两个字符
{
printf("found %s\n", str[n]);
}
}
/*
R2D2
R2A6
*/
return 0;
}
#include
#include
int main()
{
char str[] = "This is a simple string";
char* pch;
pch = strstr(str, "simple");
puts(str); // This is a simple string
puts(pch); // simple string
strncpy(pch, "sample", 6); //拼接的字符串指向同一片内存空间
puts(str); // This is a sample string
puts(pch); // sample string
return 0;
}
#include
#include
int main()
{
char str[] = "- This, a sample string.";
char* pch;
printf("%s\n", str);
pch = strtok(str, " ,.-");
while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, " ,.-");
}
/*
This
a
sample
string
*/
return 0;
}
#include
#include
#include //必须包含的头文件
int main()
{
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL)
printf("Error opening file unexist.ent: %s\n", strerror(errno));
// Error opening file unexist.ent: No such file or directory
return 0;
}
函数 | 如果他的参数符合下列条件就返回真 |
---|---|
iscntrl | 任何控制字符 |
isspace | 空白字符:空格‘ ’,换页‘\f’,换行’\n’,回车‘\r’,制表符’\t’或者垂直制表符’\v’ |
isdigit | 十进制数字 0~9 |
isxdigit | 十六进制数字,包括所有十进制数字,小写字母af,大写字母AF |
islower | 小写字母a~z |
isupper | 大写字母A~Z |
isalpha | 字母a~z 或 A~Z |
isalnum | 字母或者数字,az,AZ,0~9 |
ispunct | 标点符号,任何不属于数字或者字母的图形字符(可打印) |
isgraph | 任何图形字符 |
isprint | 任何可打印字符,包括图形字符和空白字符 |
#include
#include
struct {
char name[40];
int age;
} person, person_copy;
int main()
{
char myname[] = "Pierre de Fermat";
memcpy(person.name, myname, strlen(myname) + 1);
person.age = 46;
memcpy(&person_copy, &person, sizeof(person));
printf("person_copy: %s, %d \n", person_copy.name, person_copy.age); // person_copy: Pierre de Fermat, 46
return 0;
}
#include
#include
int main()
{
char str[] = "memmove can be very useful......";
memmove(str + 20, str + 15, 11);
puts(str); // memmove can be very useful.
return 0;
}
#include
#include
int main()
{
char buffer1[] = "DWgaOtP12df0";
char buffer2[] = "DWGAOTP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0) printf("'%s' is greater than '%s'.\n", buffer1, buffer2); // 'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.
else if (n < 0) printf("'%s' is less than '%s'.\n", buffer1, buffer2);
else printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
return 0;
}
论文介绍Two-StreamConvolutional Networks for Action Recognition in Videos
又休着假“跑”了!马斯克“抢”来的 AI 大牛离职,特斯拉自动驾驶要“黄”?...
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索
java && 类似%E4%B8%AD%E5%9B%BD这种字符转换问题
Linux企业运维——Redis部署+redis主从复制+redis主从自动切换(基于sentinel哨兵模式)+集群+redis+mysql 的缓存服务器+配置 gearman 实现数据同步
Windows下安装Bun像Node或Deno的现代JS运行时