C语言:

问题1:

关于length的几个点

结果:it okit (如果存储空间连续的话)

关于length的几个点

问题2:

int main(){

char str[] ="hello";            //字符串,后面有’\0’。所以可以不初始化长度

// char str[6] ="hello";         //不能写5,否则后果不堪设想。。

char s[] = {'a','b','c','e'};         //字符数组,后面没有’\0’

printf("%d\n",strlen(s));        //有几个就几个 这里为5,循环时用5,只是初始化的时候注意多留1空间 ,或者干脆不写数字。

printf("%d\n",sizeof(s));        //字节数+1(’\0’) 这里为6

}

 

C语言的数组可以不初始化长度,二维数组一定要初始化第二维长度

C语言中没有string类型


JAVA:所有length都是正常值,不存在+1

public static void main(String[] args) {

      String s1 ="abc d";

      String s2 = new String("abc ed");

       String s4[] = {"11","22","33"};

      String s3[] = new String[]{"11","22","33"};

/*    System.out.println(s1.length());    //

      System.out.println(s2.length());   //字符串--方法

      System.out.println(s3.length);     //数组--属性       */

      System.out.println(s1);             //s1,s2字符串的输出

      for(inti = 0;i < s3.length; i++) {//s3字符串数组的输出

         System.out.println(s3[i]);

      }

   }

 

 

public static void main(String[] args) {//必须是单个字符

      char c1 = 'a';

      char c2[] = {'a','b','c'};

      char c3[] = new char[]{'a','b','c'};

      System.out.println(s1);

      System.out.println(c2.length);

      System.out.println(c3.length);  //也是正常个数

for(inti = 0;i < c3.length; i++) {//c2,c3字符数组的输出

          System.out.println(c3[i]);

      }

   }

相关文章: