C String - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

C String

Description:

EECP0110 C Language Computer Programming C String Computer Engineering Mahanakorn University of Technology. String Compare int strcmp ( const char * str1, const char ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 45
Provided by: Tor150
Category:
Tags: string

less

Transcript and Presenter's Notes

Title: C String


1
C String
EECP0110 C Language
Computer Programming
2
Contents
Basic of String in C Language
1
2
???????????????????????????? String
3
String Input Functions
String Output Functions
4
5
String Library Functions
3
Basic 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 ????????

4
Basic of String in C Language
  • String vs. Array of char

String char color blue char color
b, l, u, e,\0 Array of char char
color b, l, u, e
5
Basic 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
...??????????????????????
8
String Input Functions
  • scanf

scanf( s, msg )
scanf ???????????????????? user
?????????????????????? ??????????? space,
newline ???? end-of-file character
9
String Input Functions
  • ?????????????????????????????????????????????????
    ????? array ???????????? ???????????????

char msg10 printf(Enter Message
) scanf(s, msg)
?
10
String Input Functions
  • ?????????????????????????????????????????????????
    ?????????? ??????????????????????????????????????

char msg10 printf(Enter Massage
) scanf(9s, msg)
11
String Input Functions
  • Edit set ( ) ??? ????????????????????????????
    ????????????????? string ??? scanf
    ????????????????????????????????????????????????
    ??? edit set ????????
  • ??. ?????????????? ???????????? ????????

char msg10 printf(Enter money
) scanf(91234567890,.s,msg)
12
String Input Functions
  • ????????????? Edit set ( )
    ????????????????????????????????? ???????????
    ?????????????? ( ) ????????????
    ?????????????????????????????? ?????? newline
  • ??????????? scanf ????????????????????????
    newline character ???????????

char msg100 printf(Enter message
) scanf(81\ns,msg)
13
String Output Functions
  • printf
  • ?????????????????????????? string

printf( s, msg )
printf( flagprecisions, string?????????? )
Left-justify
14
String Output Functions
  • printf(30s,Computer)
  • Output
  • printf(-30s,Computer)
  • Output

15
String Output Functions
  • printf(-20.15s,12345678901234567890)
  • Output

?????????????? 20 ??????? ??????????? 15 ????????
16
Standard Input/Output Library Functions
17
char 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)

18
char gets (char s)
19
int puts(const char s)
20
?????????? sprintf ??????????????????? array
21
String Library Functions
include ltstring.hgt
22
String Length
int strlen( const char string )
  • strlen ?????? ??????????????????? ????
    ???????????????????? ????????? null character
  • ???????????????????????? ?????????????

23
String Length
  • includeltstdio.hgt
  • includeltstring.hgt
  • void main()
  • char buff20
  • int num
  • strcpy(buff,What happen?)
  • num strlen(buff)
  • printf(String contains d character,num)

24
String 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
26
String Copy
  • ????????????????? String ?????? assignment
    statement ????
  • char str1 Test String
  • char str212
  • str2 str1
  • str2 Test String

27
String 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.
28
String Copy
  • ??? Copy ????????? copy ?????????????????????????
    ???????????? ?????????? /0 ??????? ????? copy

29
strcpy
30
strncpy
31
(No Transcript)
32
String 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 ???????????????????)

33
String Concatenate
34
String 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
36
String Compare
strcmp(s1,s2)
37
String Compare
strcmp(s1,s2)
38
String Compare
strcmp(s1,s2)
39
?????????????????????? strcmp ????????????????
40
?????????????????????? strcmp ????????????????
41
?????????????????????????????????????? Strncmp
strncmp(string1,string2,Size)
42
String 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)
44
Search character in String
  • char strchr ( char str, int character )

Returns a pointer to the first occurrence of
character in the C string str.
Write a Comment
User Comments (0)
About PowerShow.com