Character & String Knowledge Understand the basic concept of character and string Skills Able to write an application program to manipulating character and string
String String is a list of character. In C programming, string constant must be declared in a pair of “ “ Examples: "FTSM“ "03-8921 0001 samb 2004"
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
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’)
Memory representation for string Example: Memory representation for "S-124" 'S' '-' '1' '2' Null character '4' '\0'
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
_ Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); ? jenama
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
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
_ 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
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
Accessing String Individual character in string can be referred as an array.
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 _
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: _
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: _
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_
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_
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_
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 _
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 _
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 _
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_
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_
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 _
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_
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_
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_
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_
String assignment String cannot be directly assign to an array (except in the case of initialization of character) Example: char digit[11]; digit = "0123456789"; /* Cannot be */
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 ??
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'
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'
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'
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'
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'
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'
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'
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'
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 …
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'
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 ':'
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 ':'
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 ':'
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 ':'