1.字符数组长度和字符串长度
char a[] = “hello”;
char a[] = "hello";
int len = sizeof(a) / sizeof(char);
printf("字符数组长度:%d \n",len);
printf("字符串长度:%d \n",strlen(a));
输出内容如下,因为char数组会在末尾添加一个'\0'
,串中字符的个数成为串的长度
字符数组长度:6
字符串长度:5
2.将字符串打印出来,如果是i<len,此时是数组长度,会在末尾多输出一个空格
char a[] = "hello";
int len = sizeof(a) / sizeof(char);
for(int i=0; i<len; i++)
printf("%c",a[i]);
printf("dd\n");
for(int i=0; i<strlen(a); i++)
printf("%c",a[i]);
printf("dd");
hello dd
hellodd
字符串拼接
char a[] = "hello";
char b[] = " ";
char c[] = "world";
strcat(a,b);
strcat(a,c);
printf("%s\n",a);
3.strcat函数原型a,b字符串拼接,然后存储到a中
char* strcat(char*,const char*)