C String - PowerPoint PPT Presentation

About This Presentation
Title:

C String

Description:

C String Mahanakorn University of Technology. Mahanakorn University of Technology. Mahanakorn University of Technology. Basic of String in C Language String ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: C String


1
C String
C Language Computer
Programming
2
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 ????????

3
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
4
Basic 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
...??????????????????????
7
String Input Functions
  • scanf

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

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

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

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

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

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

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

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

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

21
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)

22
String 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
Write a Comment
User Comments (0)
About PowerShow.com