Title: C String
1C String
C Language Computer
Programming
2Basic of String in C Language
- String ?????? C ????????? Array ??? Character
?????????????????? ???????????????????????? Array
??? Character ??????? null Character(\0)
?????????????????????
char string20Initial Message
0 1 4 9 14 15 19
I n i t i a l M e s s a g e \0 ? ? ? ?
- ????????????????? null character(\0)
???????????? - ???????????????????? 15 ????????
3Basic of String in C Language
String char color blue char color
b, l, u, e,\0 Array of char char
color b, l, u, e
4Basic of String in C Language
- ??????????????? String ???????????? char
char month January
????? array ???? 5
char monthPtr January
??????????? pointer ??????????? string January
?????????????
5???????????????????????????? String
- ???????????????????????????????? String
- char msg10 Computer
- char msg10 C,o,m,p,u,t,e,r,
\0 - ??????????????????????????????????????????
String - char msg Computer
- char msg Computer"
- char msg C,o,m,p.u,t,e,r,
\0
6??? Copy String
- char str110Computer
- char str230
- str2str1
- str2Computer
?????????????????????????? String
...??????????????????????
7String Input Functions
scanf( s, msg )
scanf ???????????????????? user
?????????????????????? ??????????? space,
newline ???? end-of-file character
8String Input Functions
- ?????????????????????????????????????????????????
????? array ???????????? ???????????????
char msg10 printf(Enter Message
) scanf(s, msg)
?
9String Input Functions
- ?????????????????????????????????????????????????
?????????? ??????????????????????????????????????
char msg10 printf(Enter Massage
) scanf(9s, msg)
10String Input Functions
- Edit set ( ) ??? ????????????????????????????
????????????????? string ??? scanf
????????????????????????????????????????????????
??? edit set ???????? - ??. ?????????????? ???????????? ????????
char msg10 printf(Enter money
) scanf(91234567890,.s,msg)
11String Input Functions
- ????????????? Edit set ( )
????????????????????????????????? ???????????
?????????????? ( ) ????????????
?????????????????????????????? ?????? newline - ??????????? scanf ????????????????????????
newline character ???????????
char msg100 printf(Enter message
) scanf(81\ns,msg)
12String Output Functions
- printf
- ?????????????????????????? string
printf( s, msg )
printf( flagprecisions, string?????????? )
Left-justify
13String Output Functions
- printf(30s,Computer)
- Output
- printf(-30s,Computer)
- Output
14String Output Functions
- printf(-20.15s,12345678901234567890)
- Output
?????????????? 20 ??????? ??????????? 15 ????????
15Standard Input/Output Library Functions
16char gets (char s)
- includeltstdio.hgt
- void main()
-
- char name20
- char question20 What is your name?
- puts(question)
- gets(name)
- printf(You are s\n, name)
17char gets (char s)
18int puts(const char s)
19String Library Functions
include ltstring.hgt
20String Length
int strlen( const char string )
- strlen ?????? ??????????????????? ????
???????????????????? ????????? null character - ???????????????????????? ?????????????
21String Length
- includeltstdio.hgt
- includeltstring.hgt
- void main()
-
- char buff20
- int num
- strcpy(buff,What happen?)
- num strlen(buff)
- printf(String contains d character,num)
22String Length
- includeltstdio.hgt
- includeltstring.hgt
- void main()
-
- char buff20
- strcpy(buff,What happen?)
- printf(String contains d character,
strlen(buff))
23?????????????? strlen
include ltstdio.hgt include ltconio.hgt include
ltmalloc.hgt include ltstring.hgt void main(void)
char message int len
clrscr() message malloc(sizeof(char)256)
if(message ! NULL)
printf("Enter string ")
gets(message) len strlen(message)
printf("String length is d", len)
getch() else
printf("Out of Memory\n")
free(message)
?????????????????
Enter string Good Afternoon String length is 14