Title: C String
1C String
EECP0110 C Language
Computer Programming
2Contents
Basic of String in C Language
1
2
???????????????????????????? String
3
String Input Functions
String Output Functions
4
5
String Library Functions
3Basic 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 ????????
4Basic 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
5Basic of String in C Language
- ??????????????? String ???????????? char
char month January
????? array ???? 5
char monthPtr January
??????????? pointer ??????????? string January
?????????????
6???????????????????????????? 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
7??? Copy String
- char str110Computer
- char str230
- str2str1
- str2Computer
?????????????????????????? String
...??????????????????????
8String Input Functions
scanf( s, msg )
scanf ???????????????????? user
?????????????????????? ??????????? space,
newline ???? end-of-file character
9String Input Functions
- ?????????????????????????????????????????????????
????? array ???????????? ???????????????
char msg10 printf(Enter Message
) scanf(s, msg)
?
10String Input Functions
- ?????????????????????????????????????????????????
?????????? ??????????????????????????????????????
char msg10 printf(Enter Massage
) scanf(9s, msg)
11String Input Functions
- Edit set ( ) ??? ????????????????????????????
????????????????? string ??? scanf
????????????????????????????????????????????????
??? edit set ???????? - ??. ?????????????? ???????????? ????????
char msg10 printf(Enter money
) scanf(91234567890,.s,msg)
12String Input Functions
- ????????????? Edit set ( )
????????????????????????????????? ???????????
?????????????? ( ) ????????????
?????????????????????????????? ?????? newline - ??????????? scanf ????????????????????????
newline character ???????????
char msg100 printf(Enter message
) scanf(81\ns,msg)
13String Output Functions
- printf
- ?????????????????????????? string
printf( s, msg )
printf( flagprecisions, string?????????? )
Left-justify
14String Output Functions
- printf(30s,Computer)
- Output
- printf(-30s,Computer)
- Output
15String Output Functions
- printf(-20.15s,12345678901234567890)
- Output
?????????????? 20 ??????? ??????????? 15 ????????
16Standard Input/Output Library Functions
17char 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)
18char gets (char s)
19int puts(const char s)
20?????????? sprintf ??????????????????? array
21String Library Functions
include ltstring.hgt
22String Length
int strlen( const char string )
- strlen ?????? ??????????????????? ????
???????????????????? ????????? null character - ???????????????????????? ?????????????
23String Length
- includeltstdio.hgt
- includeltstring.hgt
- void main()
-
- char buff20
- int num
- strcpy(buff,What happen?)
- num strlen(buff)
- printf(String contains d character,num)
24String Length
- includeltstdio.hgt
- includeltstring.hgt
- void main()
-
- char buff20
- strcpy(buff,What happen?)
- printf(String contains d character,
strlen(buff))
25?????????????? 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
26String Copy
- ????????????????? String ?????? assignment
statement ???? - char str1 Test String
- char str212
- str2 str1
- str2 Test String
27String Copy
char strcpy( char Destination, const char Source )
Copies the C string pointed by Source into the array pointed by Destination, including the terminating null character
char strncpy ( char Destination, const char Source, size_t num )
Copies the first num characters of Source to Destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
28String Copy
- ??? Copy ????????? copy ?????????????????????????
???????????? ?????????? /0 ??????? ????? copy
29strcpy
30strncpy
31(No Transcript)
32String Concatenate
char strcat ( char destination, const char
source )
char strncat ( char destination, const char
source,size_t num )
- strcat ??? strncat ???????????????????????????
?????????????? ??????????????????????????????????
????? null character ??? ???????????????? - ?????? address ??? pointer ??????????????????
(address ???????????????????)
33String Concatenate
34String Compare
int strcmp ( const char str1, const char str2 )
Compares the C string str1 to the C string str2.This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached.
int strncmp ( const char str1, const char str2, size_t num )
Compares up to num characters of the C string str1 to those of the C string str2.This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
35????????????????????????????????
int strcmp ( const char str1, const char str2 ) int strncmp ( const char str1, const char str2, size_t num ) int strcmp ( const char str1, const char str2 ) int strncmp ( const char str1, const char str2, size_t num )
str1 lt str2 ??????????? 0
str1 gt str2 ?????????? 0
str1 str2 ?????????? 0
36String Compare
strcmp(s1,s2)
37String Compare
strcmp(s1,s2)
38String Compare
strcmp(s1,s2)
39?????????????????????? strcmp ????????????????
40?????????????????????? strcmp ????????????????
41?????????????????????????????????????? Strncmp
strncmp(string1,string2,Size)
42String Conversion Functions
int atoi ( const char str ) // Convert string to integer
Parses the C string str interpreting its content as an integral number, which is returned as an int value.
double atof ( const char str ) // Convert string to double
Parses the C string str interpreting its content as a floating point number and returns its value as a double.
long atol ( const char str ) // Convert string to long
Parses the C string str interpreting its content as an integral number, which is returned as a long int value.
43(No Transcript)
44Search character in String
- char strchr ( char str, int character )
Returns a pointer to the first occurrence of
character in the C string str.