1. string 1. What is a string Simple string "itcast" an 'i' is a character A lot of characters are grouped together as strings. 2. Initialization of strings char a[] = "123 "; char A [] = { ' 1 , ' 2 ' , ' Span class= "S6" >3 ' }; The difference can be compared size "123" is actually from '1','2','3',' ' composition storage distribution of "123" The output "%s" of the string, ' \ s ' is not output 3. The role of the output char a[] = {'o','k'}; "MJ" in front of Char a[] output "MJ" re-output a char a[] = {'i','t',' + ','C '}; 4. commonly used string processing functions strlen(note Chinese) 2. Exercises
Write a function char_contains (char str[],char c), if the string str contains the character C will return the value 1, otherwise return the value 0
1#include <string.h>2#include <stdio.h>3 4 //readability, Performance-lite (refactoring)5 6 intChar_contains (CharStr[],Charc);7 8 intMain ()9 {Ten CharName[] ="Itcast"; One A intresult = Char_contains (name,'C'); - -printf"%d\n", result); the return 0; - } - - //"ITC" ' 7 ' + intChar_contains (CharStr[],Charc) - { + inti =-1; A at //1. Traversing the entire string - while(Str[++i]! = C && Str[i]! =' /' ) ; - - //return str[i] = = ' 0:1 '? - returnStr[i]! =' /'; -}
Array of strings
1. Use Cases
* One -dimensional character array holds a string, such as a name char name[20] = "MJ"
* If you want to store multiple strings, such as the names of all students in a class, you need a two-dimensional character array,Char names[15][20] can hold the names of the students ( assuming the name does not exceed characters )
* If you want to store two classes of student names, then you can use a three-dimensional character array char names[2][15][20]
2. Initialize
Char names[2[x] = {{' J ',' A ',' y ',' + '}, {' J ' ,' i ',' m ','/'}};
Char names2[2[ten] = {{"Jay"}, {"Jim"}};
Char names3[2[ten] = { "Jay", "Jim" };
1#include <stdio.h>2 intMain ()3 {4 CharName[] ="Jack";5 6 CharName1[] ="Rose";7 8 CharName2[] ="Jim";9 Ten CharName3[] ="Jake"; One A Charnames[2][Ten]= {"Jack","Rose"}; - -printf"%s\n", names[0]); the -printf"%c\n", names[0][3]); - - Charnames2[2][Ten] = + { -{'J','a','C','k',' /'}, +{'R','o','s','T',' /'} A }; at - - return 0; -}
Black Horse programmer--c Language strings