Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehJefry Suci Telah diubah "10 tahun yang lalu
1
1 Character Strings
2
2 Topik String –Representation –Declaration –Functions –Kesalahan Umum –Index char dlm string
3
3 String Alami Mengingat: Main memory –Cell array yg berdekatan –Tiap cell punya alamat 0x1FFF0x20000x20010x20020x1FFE dst.
4
4 ch String Alami Recall: deklarasi variabel – “box” untuk suatu nilai Contoh: char ch; ch = ‘B’; 0x1FFF0x20000x20010x20020x1FFE dst. ‘B’
5
5 String Alami Contoh: char name[5]; Menentukan jmlh Cell dalam array Deklarasi String –set array of cells –Tiap cell berisi char –address pertama cell dlm array
6
6 String Alami Deklarasi String –set array of cells –Tiap cell berisi char –address pertama cell dlm array Contoh: char name[5]; 0x2000 0x2004 name is 0x2000
7
7 Character String Deklarasi 1: char name[5]; Deklarasi 2: #define MAXLENGTH 5 char name[MAXLENGTH]; 0x2000 0x2004 name is 0x2000
8
8 String Input/Output #include #define MAXLENGTH 15 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; scanf("%s %s", string1, string2); printf("%s %s\n", string1, string2); return 0; } Tidak ada ampersand (&)!
9
9 Character Strings Initialization: char nama[5] = “Ann”; Ann\0 Terminating Character: Tandai akhir string Special char: ’\0’ disbt NUL (single L) 0x2000 0x2004 nama is 0x2000
10
10 Character Strings Initialization: char name[5] = “Ann”; Dpt menyimpan setidaknya 4 huruf, Krn ada `\0’ Ann\0 0x2000 0x2004 name is 0x2000
11
11 Character Strings Deklarasi 3: char nama[] = “Ann”; memerlukan extra cell utk‘\0’ Ann\0 0x2000 0x2003 nama is 0x2000
12
12 Character Strings Deklarasi 4: char *nama = “Ann”; Hasilnya “undefined” Jika kita mencoba memodifikasi String ini. Ann\0 0x3000 0x3003 0x3000 nama
13
13 Character Strings Deklarasi 5: char nama[]; String dg panjang tidak tentu ? Tidak! Ini menyebabkan error.
14
14 Char dalam String Ukuran character string adalah tetap/fixed. Character pada posisi index: –string[index] –Karakter pertama ber-index 0
15
15 char nama[8] = “John”; int i = 2; printf(“Char pd index %d adlh %c.\n”, i, nama[i]); Char dalam String output: Char pd index 2 adlh h. index 0index 4 John\0 0x39950x399C nama is 0x3995
16
16 Char dalam String index 2 John\0 0x39950x399C nama is 0x3995 char nama[8] = “John”; nama[2] = ‘X’; printf(“Nama: %s\n”, nama);
17
17 JoXn\0 0x39950x399C name is 0x3995 output: Nama: JoXn index 2 char name[8] = “John”; name[2] = ‘X’; printf(“Name: %s\n”, name); A Char in a String
18
18 Operasi String #include Operasi: –Assignment: strcpy() –Concatenation: strcat() –Comparison: strcmp() –Length: strlen()
19
19 #include #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } Operasi String: Assignment string1: string2:
20
20 Operasi String: Assignment #include #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } string1: “Hello World!” string2:
21
21 Operasi String: Assignment #include #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } string1: “Hello World!” string2: “Hello World!”
22
22 char nama1[5] = “Ann”; char nama2[5] = “Dave”; nama2 = nama1; Kesalahan umum Wrong Assignment Contoh 1: Error: “LValue required....”
23
23 char *nama1 = “Ann”; char *nama2 = “Dave”; nama2 = nama1; Kesalahan umum Bad Assignment Contoh 2: Lebih baik menghindari inisialisasi dg cara ini (Biasanya, tidak ada pesan error)
24
24 Kesalahan umum Bad Assignment Dave\0 0x39900x3994 Ann\0 0x20000x2003 0x2000 nama1 0x3990 nama2 char *nama1 = “Ann”; char *nama2 = “Dave”;
25
25 Kesalahan umum Bad Assignment Dave\0 0x39900x3994 Ann\0 0x20000x2003 0x2000 nama1 0x2000 nama2 nama2 = nama1;
26
26 Kesalahan umum Tidak cukup ruang Ann\0 0x2000 0x2003 char nama[] = “Ann”; strcpy(nama, “David”); nama is 0x2000
27
27 Common Mistake: Not enough space David\0 0x2000 0x2003 char name[] = “Ann”; strcpy(name, “David”); Perlu di perhatikan name is 0x2000
28
28 Operasi String: Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”); string1: “Goodbye” string2: “, Cruel “
29
29 char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”); string1: “Goodbye, Cruel ” string2: “, Cruel “ Operasi String : Concatenation
30
30 string1: “Goodbye, Cruel, Cruel ” string2: “, Cruel “ Operasi String : Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”);
31
31 string1: “Goodbye, Cruel, Cruel World!” string2: “, Cruel “ Operasi String : Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, “World!”);
32
32 Kesalahan umum char nama[5]; strcpy(nama, “Ann”); strcat(nama, “ Smith”); Tidak cukup ruang Ann\0 0x2000 0x2004 nama is 0x2000
33
33 Kesalahan umum char nama[5]; strcpy(nama, “Ann”); strcat(nama, “ Smith”); Tidak cukup ruang AnnSmit 0x2000 0x2004 h\0 nama is 0x2000
34
34 strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (strcmp(string1, string2) < 0) { printf(“%s %s\n”, string1, string2); } else { printf(“%s %s\n”, string2, string1); } output: Apple Wax String Operation: Comparison
35
35 strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (strcmp(string1, string2) < 0) { printf(“%s %s\n”, string1, string2); } else { printf(“%s %s\n”, string2, string1); } Operasi String: Comparison Returns: negative jika string1 < string2 zero jika string1 == string2 Positive jika string1 > string2
36
36 strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (string1 < string2) { printf(“%s %s\n”, string1, string2); } else { printf(“%s %s\n”, string2, string1); } Kesalahan Umum Wrong Comparison
37
37 output: 5 Jumlah char Sblm `\0’. char string1[100]; strcpy(string1, “Apple”); printf(“%d\n”, strlen(string1)); Operasi String: Length
38
38 Kesalahan umum char nama[5]; strcpy(nama, “David”); Tidak cukup ruang Jangan Lupa ‘\0’. David\0 0x39900x3994 nama is 0x3990
39
39 Character String sbg Parameter String sbg parameter formal dideklarasikan sbg char* atau char[] –Contoh: void Greet ( char* name ) void Greet ( char name[] ) Sbg pointer pada elemen pertama dari string (array of chars). Perubahan pada string di dalam function berpengaruh pada string aktual.
40
40 Contoh: hello3.c #include #define NAMELEN 50 /* Cetak pesan simpel user. */ void Greet ( char * name ) { strcat(name, "! Apa Kabar?"); } int main() { char user[NAMELEN]; printf("Apa Kabar? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } user Jake\0
41
41 int main() { char user[NAMELEN]; printf("Apa Kabar? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Contoh: hello3.c #include #define NAMELEN 50 /* Cetak pesan simpeluser. */ void Greet ( char * name ) { strcat(name, "! Apa Kabar?"); } name user Jake\0
42
42 int main() { char user[NAMELEN]; printf("Apa Kabar? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Contoh: hello3.c #include #define NAMELEN 50 /* Cetak pesan simpeluser. */ void Greet ( char * name ) { strcat(name, "! Apa Kabar?"); } user Jake! Apa Kabar?\0 name
43
43 int main() { char user[NAMELEN]; printf("Apa Kabar? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Contoh: hello3.c #include #define NAMELEN 50 /* Cetak pesan simpeluser. */ void Greet ( char * name ) { strcat(name, "! Apa Kabar?"); } user Jake! Apa Kabar?\0
44
44 int main() { char user[NAMELEN]; printf("Apa Kabar? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } Lebih lanjut dengan scanf Tidak ada (&) dlm scanf dg string!
45
45 Kesimpulan String = array character yg berdekatan String identifier adalah alamat karakter pertama dalam string. Karakter tungal dlm string di akses dg notasi str[index] Ada pustaka fungsi C utk meng-copy, concatenating dan comparing string.
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.