An array of int float double
char array of characters
First, the array of characters
1 , generally in the form of an array of characters
char array name [ constant expression ]
char cArray[5];
2 , reference character array is the same as with other types, with the subscript.
char cArray[5];
cArray[0]='H';
CArray [1] = 'and';
cArray[2]='l';
cArray[3]='l';
CArray [4] = 'a';
3 , initialize the array of characters
(1) to initialize the elements one by one can be omitted index initialization.
CArray char [5] = {H ',' e ',' l ',' l ',' o '};
CArray char [] = {H ',' e ',' l ',' l ',' o '};
(2) a string for array assignment, initialization can be omitted subscripts
char cArray[5] = {"hello"};
char cArray[]={"hello"};
char cArray [] = "hello" ; ( most common )
Note: 1 , the use of strings is initialized, the system will automatically add an array inside a '\ 0' , as the end of the string flag
char cArray [] = "hello" ; equivalent to char cArray [] = { 'H ', 'e', 'l', 'l', 'o', '\ 0'};
printf("%d\n", sizeof(cArray));
printf("%d\n", sizeof(cArray1));
Note: 2 , a string of bytes Chinese Chinese is 2.
printf("%d", sizeof("九夏"));// \0
3, the initialization string, array sizes large number of elements than we need.
For example, the length of the name is not the same
4 , the input and output array of characters
( 1 ) a % c input by one output
Enter the number of characters greater than the number of array size, only accept the front.
1 #include <stdio.h> 2 3 int main() 4 { 5 char cArray[5]; 6 for (int i = 0; i < 5; i++) 7 { 8 scanf("%c", &cArray[i]); 9 } 10 11 for (int i = 0; i < 10; i++) 12 { 13 printf("%c", cArray[i]); 14 } 15 16 printf("\n"); 17 return 0; 18 }
( 2 ) with a % s overall input and output, the input space is truncated
1 #include <stdio.h> 2 3 int main() 4 { 5 char cArray[20]; 6 scanf("%s", &cArray); 7 8 printf("%s", cArray); 9 return 0; 10 }
1 #include <stdio.h> 2 3 int main() 4 { 5 //%c 6 7 /*int Row, Col; 8 char cArray[2][5]; 9 10 for (Row = 0; Row < 2; Row++) 11 { 12 for (Col = 0; Col < 5;Col++) 13 { 14 scanf("%c", &cArray[Row][Col]); 15 } 16 } 17 18 for (Row = 0; Row < 2; Row++) 19 { 20 for (Col = 0; Col < 5; Col++) 21 { 22 printf("%c", cArray[Row][Col]); 23 } 24 }*/ 25 26 27 //%s 28 int Row, Col; 29 char cArray[11]; 30 scanf("%s", &cArray); 31 printf("%s", cArray); 32 33 return 0; 34 }
Second, String Functions
First you need to include the header file strings: string.h
1 copy
strcpy ( target string, the source string );
Copy the source string into the target string.
( 1 ) the target must be a string variable.
( 2 ) the source string is not necessarily.
( 3 ) Even '\ 0' copy together , the first '\ 0' end
1 Code: 2 // copy string Assume a string of character array str1 [] str2 [] . 3 . 4 // char str1 [30] = {0}; // 0 27 months \ 0 \ 0 \ 0 . 5 / / char str2 [30] = "hellosadfsadfgoisdfgsdkfg"; // . 6 characters . 6 . 7 // the printf ( "% D", the sizeof (str2)); . 8 . 9 10 // for (int I = 0; I <the sizeof (str2 ); I ++) . 11 // { 12 is // str1 [I] = str2 [I]; 13 is // } 14 15 // the printf ( "% S \ n-", str1); 16 . 17 18 is . 19 // char str1 [] = "ABCDEFG"; // index memory for the decision variables // \ 0 End 20 is @ 21 is 22 is // strcpy (str1, "FGH"); // \ 0 23 is 24 // the printf ( "C% \ n-", str1 [0]); 25 // the printf ( "% C \ n-", str1 [. 1]); 26 is // the printf ( "% C \ n-", str1 [2]); 27 // the printf ( "% C \ n-", str1 [. 3]); 28 // the printf ( "% C \ n-", str1 [. 4]); 29 // the printf ( "% C \ n-", str1 [ . 5]); 30 // the printf ( "% C \ n-", str1 [. 6]); 31 32 33 //printf("%s\n", str1);
2 , more
strcmp ( string 1 and string 2);
( 1 ) String 1 == string 2 returns 0
( 2 ) string 1> string 2 return 1
( 3 ) string 1 < string 2 returns -1
1 //字符串的比较 2 //char str1[100], str2[100]; 3 4 //while (1) 5 //{ 6 // int i = 0, re; 7 // scanf("%s", &str1); 8 // scanf("%s", &str2); 9 10 // while (str1[i]) 11 // { 12 // if (str1[i] != str2[i]) //str1[0]----->str1[99] 13 // break; 14 // else 15 // i++; 16 // } 17 18 // if (str1[i] > str2[i]) 19 // { 20 // printf("str1>str2\n"); 21 // } 22 // else if (str1[i] < str2[i]) 23 // { 24 // printf("str1<str2\n"); 25 // } 26 // else 27 // { 28 // printf("str1=str2\n"); 29 // } 30 //} 31 32 33 34 /*int num = 1; 35 36 char str1 [30] = "Hello"; 37 [ char str2 [30]; 38 is 39 the printf ( "To enter the system Enter password: \ n-"); 40 the while (. 1) 41 is { 42 is Scanf ( "% S", & str2 ); 43 is IF (strcmp (str1, str2) == 0) 44 is { 45 the printf ( "Welcome to the system, although the system is still in development ..."); 46 is } 47 the else IF (strcmp (str1, str2 !) = 0) 48 { 49 NUM ++; 50 the printf ( "wrong password, this is the% d warnings \ n-", NUM); 51 is IF (NUM ==. 4) 52 is { 53 is the printf ( ". 3 times error, Contact People's Liberation Army \ n "); 54 break; 55 } 56 57 continue; 58 } 59 60 }
3, Get string length
// sizeof (); // byte
// strlen (); // true ( effective ) length
char a[30] = "hello";
printf("%d\n", sizeof(a));//30
printf("%d\n", strlen(a)); //5