Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]);

Similar presentations


Presentation on theme: "Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]);"— Presentation transcript:

1 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 _

2 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 _

3 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 _

4 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 _

5 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 _

6 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 _

7 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 _

8 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 _

9 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 _

10 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 _

11 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 _

12 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 _

13 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 _

14 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 _

15 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 _

16 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 _

17 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 _

18 Character Manipulation Function
Special function was provided in standard library C for manipulating character. Refer to Jadual 12.1 (page 333) in Pengaturcaraan C

19 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

20 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

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

22 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?

23 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

24 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

25 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

26 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!!!

27 getchar() and putchar()
getchar() dan putchar() can be used for input and output character

28 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:

29 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

30 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”

31 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

32 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

33 gets() and puts() These functions gets() dan puts() can be used for input and output string

34 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

35 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

36 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

37 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

38 String manipulation functions
String manipulation also provided with C. Such as : - strcpy() strlen() strcat() strcmp()

39 strcpy() strcpy() copies the contents of one string to another string.
Prototype: char *strcpy(char sasaran[], char sumber[]);

40 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 _

41 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: _

42 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 _

43 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 _

44 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: _

45 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_

46 strlen() strlen() is a function returns the length of string. If the string is empty, it returns zero. Prototype: int strcpy(char rentetan[]);

47 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 _

48 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 _

49 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 _

50 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 _

51 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 _

52 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 _

53 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 _

54 strcat() strcat() append first string into the end of a second string
Prototype : char *strcat(char s1[], char s2[]);

55 #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 _

56 #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 _

57 #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 _

58 #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 _

59 #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_

60 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

61 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 _

62 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 _

63 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 _

64 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 _

65 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 _

66 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 _

67 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 _


Download ppt "Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]);"

Similar presentations


Ads by Google