C language-06 complex data type-02 string,-06-02
#include <stdio.h>
int main ()
{
// char name [] = {'i', 't', 'c', 'H', 's', 't', '\ 0'};
char name [] = "itcast";
name [3] = 'H';
/ *
int size = sizeof (name);
printf ("% d \ n", size);
* /
printf ("I'm taking a class at% s \ n", name);
return 0;
}
// an initialization of the string
void test2 ()
{
// \ 0's ASCII value is 0
// all strings
char name [8] = "it";
char name2 [8] = {'i', 't', '\ 0'};
char name3 [8] = {'i', 't', 0};
char name4 [8] = {'i', 't'};
// not a string (only a character array)
char name5 [] = {'i', 't'};
}
/ *
void test ()
{
// 'a' 'b' 'A'
// "jack" == 'j' + 'a' + 'c' + 'k' + '\ 0'
char name [10] = "jack888 \ n";
// Passing the array is just a warning
printf (name);
printf (name);
printf (name);
printf ("57843578435");
} * /
#include <stdio.h>
/ *
The role of \ 0
1. End of string tag
2.printf ("% s", name2);
Will output characters from the address name2 until it encounters \ 0
* /
int main ()
{
char name [] = "itc \ 0ast";
char name2 [] = {'o', 'k'};
// printf ("% s \ n", name2);
printf ("% s \ n", & name2 [1]);
return 0;
}
/ *
strlen function: Calculate the length of a string
1. Calculate the number of characters, not the number of words. A Chinese character counts as 3 characters
2. Calculated characters do not include \ 0
3. Count the number of characters from an address until it encounters \ 0
* /
// strlen function is declared in string.h file
#include <string.h>
#include <stdio.h>
int main ()
{
// int size = strlen ("哈 haha");
// printf ("% d \ n", size);
/ *
char name [] = "itcast";
char name2 [] = {'0', '\ 0', '6'};
int size = strlen (name2);
printf ("% d \ n", size);
* /
char name [] = "itcast";
// printf ("% s \ n", name);
printf ("% c \ n", name [3]);
return 0;
}
String array
#include <stdio.h>
int main ()
{
char name [] = "jack";
char name1 [] = "rose";
char name2 [] = "jim";
char name3 [] = "jake";
char names [2] [10] = {"jack", "rose"};
// printf ("% s \ n", names [0]);
// printf ("% c \ n", names [0] [3]);
char names2 [2] [10] =
{
{'j', 'a', 'c', 'k', '\ 0'},
{'r', 'o', 's', 't', '\ 0'}
};
return 0;
}