Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; ? str1 str2 _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 Selamat _ void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 Selamat _ void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 1 i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 1 i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 1 i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 1 i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 1 i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 1 i Selamat _
Example : Until … 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber Until … sasaran 2 i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 7 i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 7 i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 7 i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 ? str2 sumber sasaran 7 i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 str2 sumber sasaran 7 i void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 str2 sumber sasaran 7 i Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 str2 Selamat _ void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 str2 Selamat _
Example : 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 str2 Selamat _ void salin(char sasaran[], char sumber[]); int main() { char str1[8], str2[8]; scanf("%s", str1); salin(str2, str1); printf("Salinan: %s\n", str2); } void salin(char sasaran[ ], char sumber[ ]) { int i = 0; while (sumber[i] != '\0') { sasaran[i] = sumber[i]; i++; sasaran[i] = '\0'; 'S' 'e' 'l' 'a' 'm' 't' '\0' str1 str2 Selamat _
Character Manipulation Function Special function was provided in standard library C for manipulating character. Refer to Jadual 12.1 (page 333) in Pengaturcaraan C
Example: Input one word #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); Input one word
Example : Copy every character #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); Copy every character
Example: End of string character? #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); End of string character?
Example : Is this small alphabet? #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); Is this small alphabet?
Example: If yes, assign capital letter for #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); If yes, assign capital letter for that character into array str2
Example: If not, assign that character into array str2 #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); If not, assign that character into array str2
Example: Next index #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); Next index
Example: Don’t forget null character!!! #include <stdio.h> #include <ctype.h> int main() { char str1[8], str2[8]; int x = 0; printf("Masukkan satu perkataan: "); scanf("%s", str1); while (str1[x] != '\0') { if (islower(str1[x])) str2[x] = toupper(str1[x]); else str2[x] = str1[x]; x++; } str2[x] = '\0'; printf("%s dalam huruf besar: %s\n", str1, str2); Don’t forget null character!!!
getchar() and putchar() getchar() dan putchar() can be used for input and output character
Example : Read character by character into an array: #include <stdio.h> #define MAKS 8 int main() { char str[MAKS]; int x = 0; do { str[x] = getchar(); x++; } while (x < MAKS && str[x-1] != '\n'); str[x-1] = '\0'; x = 0; while (str[x] != '\0') { putchar(str[x]); } Read character by character into an array:
Example: Read next character #include <stdio.h> #define MAKS 8 int main() { char str[MAKS]; int x = 0; do { str[x] = getchar(); x++; } while (x < MAKS && str[x-1] != '\n'); str[x-1] = '\0'; x = 0; while (str[x] != '\0') { putchar(str[x]); } Read next character
Example: #include <stdio.h> #define MAKS 8 int main() { char str[MAKS]; int x = 0; do { str[x] = getchar(); x++; } while (x < MAKS && str[x-1] != '\n'); str[x-1] = '\0'; x = 0; while (str[x] != '\0') { putchar(str[x]); } Loop until there are no more items in array and before users pushing “enter”
Example: Don’t forget to insert null character into an array #include <stdio.h> #define MAKS 8 int main() { char str[MAKS]; int x = 0; do { str[x] = getchar(); x++; } while (x < MAKS && str[x-1] != '\n'); str[x-1] = '\0'; x = 0; while (str[x] != '\0') { putchar(str[x]); } Don’t forget to insert null character into an array
Example: Display an output for character by character inside an array #include <stdio.h> #define MAKS 8 int main() { char str[MAKS]; int x = 0; do { str[x] = getchar(); x++; } while (x < MAKS && str[x-1] != '\n'); str[x-1] = '\0'; x = 0; while (str[x] != '\0') { putchar(str[x]); } Display an output for character by character inside an array
gets() and puts() These functions gets() dan puts() can be used for input and output string
Example: Read two strings #include <stdio.h> int main() { char str1[5], str2[11]; int x, y; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); x = 0; while (str2[x] != '\0') x++; y = 0; while (str1[y] != '\0') { str2[x] = str1[y]; y++; } str2[x] = '\0'; printf("Hasil selepas cantuman: "); puts(str2); Read two strings
Example: Find index for null character in str2 #include <stdio.h> int main() { char str1[5], str2[11]; int x, y; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); x = 0; while (str2[x] != '\0') x++; y = 0; while (str1[y] != '\0') { str2[x] = str1[y]; y++; } str2[x] = '\0'; printf("Hasil selepas cantuman: "); puts(str2); Find index for null character in str2
Example: Insert every character in Str1 at the end of string in str2 #include <stdio.h> int main() { char str1[5], str2[11]; int x, y; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); x = 0; while (str2[x] != '\0') x++; y = 0; while (str1[y] != '\0') { str2[x] = str1[y]; y++; } str2[x] = '\0'; printf("Hasil selepas cantuman: "); puts(str2); Insert every character in Str1 at the end of string in str2
Example: Don’t forget null character #include <stdio.h> int main() { char str1[5], str2[11]; int x, y; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); x = 0; while (str2[x] != '\0') x++; y = 0; while (str1[y] != '\0') { str2[x] = str1[y]; y++; } str2[x] = '\0'; printf("Hasil selepas cantuman: "); puts(str2); Don’t forget null character
String manipulation functions String manipulation also provided with C. Such as : - strcpy() strlen() strcat() strcmp()
strcpy() strcpy() copies the contents of one string to another string. Prototype: char *strcpy(char sasaran[], char sumber[]);
Example: ? #include <stdio.h> asal int main() { char asal[8], salinan[8]; printf("Masukkan rentetan: "); gets(asal); strcpy(salinan, asal); printf("Salinan: "); puts(salinan); } ? asal ? salinan _
Example: ? #include <stdio.h> asal int main() { char asal[8], salinan[8]; printf("Masukkan rentetan: "); gets(asal); strcpy(salinan, asal); printf("Salinan: "); puts(salinan); } ? asal ? salinan Masukkan rentetan: _
Example: #include <stdio.h> int main() { char asal[8], salinan[8]; printf("Masukkan rentetan: "); gets(asal); strcpy(salinan, asal); printf("Salinan: "); puts(salinan); } 'k' 'u' 'r' 's' '\0' ? asal ? salinan Masukkan rentetan: kursus _
Example: #include <stdio.h> int main() { char asal[8], salinan[8]; printf("Masukkan rentetan: "); gets(asal); strcpy(salinan, asal); printf("Salinan: "); puts(salinan); } 'k' 'u' 'r' 's' '\0' ? asal 'k' 'u' 'r' 's' '\0' ? salinan Masukkan rentetan: kursus _
Example: #include <stdio.h> int main() { char asal[8], salinan[8]; printf("Masukkan rentetan: "); gets(asal); strcpy(salinan, asal); printf("Salinan: "); puts(salinan); } 'k' 'u' 'r' 's' '\0' ? asal 'k' 'u' 'r' 's' '\0' ? salinan Masukkan rentetan: kursus Salinan: _
Example: #include <stdio.h> int main() { char asal[8], salinan[8]; printf("Masukkan rentetan: "); gets(asal); strcpy(salinan, asal); printf("Salinan: "); puts(salinan); } 'k' 'u' 'r' 's' '\0' ? asal 'k' 'u' 'r' 's' '\0' ? salinan Masukkan rentetan: kursus Salinan: kursus_
strlen() strlen() is a function returns the length of string. If the string is empty, it returns zero. Prototype: int strcpy(char rentetan[]);
Example : ? str1 ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else printf(“rentetan sama panjang\n"); } ? str1 ? str2 _
Example: 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else printf(“rentetan sama panjang\n"); } 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 Masukkan rentetan 1: bola Masukkan rentetan 2: bebola _
Example: 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else printf(“rentetan sama panjang\n"); } 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 Masukkan rentetan 1: bola Masukkan rentetan 2: bebola _
Example : 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else printf(“rentetan sama panjang\n"); } 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 4 6 Masukkan rentetan 1: bola Masukkan rentetan 2: bebola _
Example: 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else printf(“rentetan sama panjang\n"); } 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 Masukkan rentetan 1: bola Masukkan rentetan 2: bebola _
Example: 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else printf(“rentetan sama panjang\n"); } 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 Masukkan rentetan 1: bola Masukkan rentetan 2: bebola panjang rentetan 1 < rentetan 2 _
Example: 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strlen(str1) > strlen(str2)) printf(“panjang rentetan 1 > rentetan 2\n"); else if (strlen(str1) < strlen(str2)) printf(“panjang rentetan 1 < rentetan 2\n"); else printf(“rentetan sama panjang\n"); } 'b' 'o' 'l' 'a' '\0' ? str1 'b' 'e' 'o' 'l' 'a' '\0' ? str2 Masukkan rentetan 1: bola Masukkan rentetan 2: bebola panjang rentetan 1 < rentetan 2 _
strcat() strcat() append first string into the end of a second string Prototype : char *strcat(char s1[], char s2[]);
#include <stdio.h> int main() { char str1[5], str2[11]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); strcat(str2, " "); strcat(str2, str1); printf("Hasil selepas cantuman: "); puts(str2); } ? str2 ? str1 _
#include <stdio.h> int main() { char str1[5], str2[11]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); strcat(str2, "/"); strcat(str2, str1); printf("Hasil selepas cantuman: "); puts(str2); } '3' '1' '/' '0' '8' '\0' ? str2 '1' '9' '5' '7' '\0' str1 Masukkan rentetan 1: 1957 Masukkan rentetan 2: 31/08 _
#include <stdio.h> int main() { char str1[5], str2[11]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); strcat(str2, "/"); strcat(str2, str1); printf("Hasil selepas cantuman: "); puts(str2); } '3' '1' '/' '0' '8' '\0' ? str2 '1' '9' '5' '7' '\0' str1 Masukkan rentetan 1: 1957 Masukkan rentetan 2: 31/08 _
#include <stdio.h> int main() { char str1[5], str2[11]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); strcat(str2, "/"); strcat(str2, str1); printf("Hasil selepas cantuman: "); puts(str2); } '3' '1' '/' '0' '8' '9' str2 '5' '7' '\0' '1' '9' '5' '7' '\0' str1 Masukkan rentetan 1: 1957 Masukkan rentetan 2: 31/08 _
#include <stdio.h> int main() { char str1[5], str2[11]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); strcat(str2, "/"); strcat(str2, str1); printf("Hasil selepas cantuman: "); puts(str2); } '3' '1' '/' '0' '8' '9' str2 '5' '7' '\0' '1' '9' '5' '7' '\0' str1 Masukkan rentetan 1: 1957 Masukkan rentetan 2: 31/08 Hasil selepas cantuman: 31/08/1957_
strcmp() strcmp() compares two string until unequal characters are found or until the end of the string is reached. Prototype: int strcmp(char s1[], char s2[]); Return value strcmp(): If s1 < s2, so strcmp(s1, s2) < 0 If s1 > s2, so strcmp(s1, s2) > 0 If s1 equal to s2, so strcmp(s1, s2) == 0
Example: ? str1 ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } ? str1 ? str2 _
Example : 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 Masukkan rentetan 1: Wira Masukkan rentetan 2: Iswara _
Example : 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 Masukkan rentetan 1: Wira Masukkan rentetan 2: Iswara _
Example : 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 Masukkan rentetan 1: Wira Masukkan rentetan 2: Iswara _
Example : 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 Masukkan rentetan 1: Wira Masukkan rentetan 2: Iswara _
Example : 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 Masukkan rentetan 1: Wira Masukkan rentetan 2: Iswara rentetan 1 > rentetan 2 _
Example : 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 #include <stdio.h> int main() { char str1[8], str2[8]; printf("Masukkan rentetan 1: "); gets(str1); printf("Masukkan rentetan 2: "); gets(str2); if (strcmp(str1, str2) < 0) printf("rentetan 1 < rentetan 2\n"); else if (strcmp(str1, str2) > 0) printf("rentetan 1 > rentetan 2\n"); else printf("rentetan 1 bersamaan rentetan 2\n"); } 'W' 'i' 'r' 'a' '\0' ? str1 'I' 's' 'w' 'a' 'r' '\0' ? str2 Masukkan rentetan 1: Wira Masukkan rentetan 2: Iswara rentetan 1 > rentetan 2 _