Download presentation
Presentation is loading. Please wait.
1
Character & String Knowledge
Understand the basic concept of character and string Skills Able to write an application program to manipulating character and string
2
String String is a list of character.
In C programming, string constant must be declared in a pair of “ “ Examples: "FTSM“ " samb 2004"
3
Memory representation for string
Example (array of character) char model[ ] = {'S', '-', '1', '2', '4', '\0'}; [0] 'S' [1] '-' [2] '1' [3] '2' [4] '4' [5] '\0' model
4
Memory representation for string
In C, memory representation for string can be depicted by an array of character array. Bear in mind, the last character in character string must be null character(i.e ‘\0’)
5
Memory representation for string
Example: Memory representation for "S-124" 'S' '-' '1' '2' Null character '4' '\0'
6
Memory representation for string
Compare the memory representation for char model[ ] = {'S', '-', '1', '2', '4', '\0'}; [0] 'S' [1] '-' [2] '1' [3] '2' [4] '4' [5] '\0' model
7
_ Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); ? jenama
8
Masukkan jenama: Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); ? jenama
9
Masukkan jenama: Singer
Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); 'S' 'i' 'n' 'g' 'e' 'r' '\0' ? jenama
10
_ Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); 'M' 'i' 't' 's' 'u' 'b' 'h' '\0' ? jenama
11
String output Output of String
Jenama: Mitsubishi _ String output Output of String Output specification for string character is %s . Example : char jenama[15] = "Mitsubishi"; printf(“Jenama: %s\n", jenama); 'M' 'i' 't' 's' 'u' 'b' 'h' '\0' ? jenama
12
Accessing String Individual character in string can be referred as an array.
13
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i _
14
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i Nama: _
15
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i Nama: _
16
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i Nama: A_
17
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 1 Nama: A_
18
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 1 Nama: A_
19
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 1 Nama: A _
20
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 2 Nama: A _
21
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 2 Nama: A _
22
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 2 Nama: A B_
23
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } Until … 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 3 Nama: A B_
24
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 6 Nama: A Bin _
25
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 6 Nama: A Bin B_
26
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 7 Nama: A Bin B_
27
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 7 Nama: A Bin B_
28
Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: ");
while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 7 Nama: A Bin B_
29
String assignment String cannot be directly assign to an array (except in the case of initialization of character) Example: char digit[11]; digit = " "; /* Cannot be */
30
String assignment Item by item Example: char digit[15], aksara;
for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; ?? digit aksara ??
31
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; ?? digit aksara '0'
32
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; ?? digit aksara '0'
33
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' ?? digit aksara '0'
34
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' ?? digit aksara '1'
35
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' ?? digit aksara '1'
36
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' ?? digit aksara '1'
37
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' ?? digit aksara '2'
38
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' ?? digit aksara '2'
39
String assignment Item by item Example: Until …
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' ?? digit aksara '2' Until …
40
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ?? digit aksara '9'
41
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ?? digit aksara ':'
42
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ?? digit aksara ':'
43
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '\0' ?? digit aksara ':'
44
String assignment Item by item Example: char digit[15], aksara;
Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '\0' ?? digit aksara ':'
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.