/*
strlen函数: 计算字符串长度
1.计算的是字符数,并不是字数
2.计算的字符不包括\0
*/
//strlen函数声明在string.h文件中
#include <string.h>
#include <stdio.h>
int main()
{
//int size = strlen("haha");
//printf("%d\n",size );
/*
char name[] = "itcast";
int size = strlen(name);
printf("%d\n",size );// 6
char name2[] = "it\0cast";
int size2 = strlen(name2);
printf("%d\n",size2 );// 2
*/
char name[] = "itcast";
char name2[] = {'0','6'};
int size2 = strlen(name2);
printf("%d\n",size2 );// 8,因为strlen从name2地址开始数字符,直到遇到\0结束
char name[] = "itcast";
printf("%c\n",name[2]); //输出为a
}
3、二维数组的使用
char names[2][10]={"jack","rose"};
printf("%s\n",names[0]); //输出jack
printf("%c\n",names[0][3]);//输出k,注意不用地址符&
char name2[2][10]=
{
{'j','a','c','k','\0'},
{'r','o','s','e','\0'}
};
代码的编写注意点:可读性 —> 性能 -> 精简(重构)不是代码越少,性能越高